use a smaller packetHandler interface

The packetHandler interface just needs two methods: one for handling
packets, and one for closing.
This commit is contained in:
Marten Seemann 2018-05-21 09:19:16 +08:00
parent ef34d9e85f
commit b3fd768a61
11 changed files with 147 additions and 142 deletions

View file

@ -42,7 +42,7 @@ type client struct {
handshakeChan chan struct{}
session packetHandler
session quicSession
logger utils.Logger
}
@ -483,7 +483,7 @@ func (c *client) createNewGQUICSession() (err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
runner := &runner{
onHandshakeCompleteImpl: func(_ packetHandler) { close(c.handshakeChan) },
onHandshakeCompleteImpl: func(_ Session) { close(c.handshakeChan) },
removeConnectionIDImpl: func(protocol.ConnectionID) {},
}
c.session, err = newClientSession(
@ -508,7 +508,7 @@ func (c *client) createNewTLSSession(
c.mutex.Lock()
defer c.mutex.Unlock()
runner := &runner{
onHandshakeCompleteImpl: func(_ packetHandler) { close(c.handshakeChan) },
onHandshakeCompleteImpl: func(_ Session) { close(c.handshakeChan) },
removeConnectionIDImpl: func(protocol.ConnectionID) {},
}
c.session, err = newTLSClientSession(

View file

@ -28,7 +28,7 @@ var _ = Describe("Client", func() {
addr net.Addr
connID protocol.ConnectionID
originalClientSessConstructor func(connection, sessionRunner, string, protocol.VersionNumber, protocol.ConnectionID, *tls.Config, *Config, protocol.VersionNumber, []protocol.VersionNumber, utils.Logger) (packetHandler, error)
originalClientSessConstructor func(connection, sessionRunner, string, protocol.VersionNumber, protocol.ConnectionID, *tls.Config, *Config, protocol.VersionNumber, []protocol.VersionNumber, utils.Logger) (quicSession, error)
)
// generate a packet sent by the server that accepts the QUIC version suggested by the client
@ -49,7 +49,7 @@ var _ = Describe("Client", func() {
connID = protocol.ConnectionID{0, 0, 0, 0, 0, 0, 0x13, 0x37}
originalClientSessConstructor = newClientSession
Eventually(areSessionsRunning).Should(BeFalse())
// sess = NewMockPacketHandler(mockCtrl)
// sess = NewMockQuicSession(mockCtrl)
addr = &net.UDPAddr{IP: net.IPv4(192, 168, 100, 200), Port: 1337}
packetConn = newMockPacketConn()
packetConn.addr = &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1234}
@ -104,9 +104,9 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
remoteAddrChan <- conn.RemoteAddr().String()
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run()
return sess, nil
}
@ -128,9 +128,9 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
hostnameChan <- h
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run()
return sess, nil
}
@ -159,8 +159,8 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
sess := NewMockPacketHandler(mockCtrl)
) (quicSession, error) {
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run().Do(func() { close(run) })
sess.EXPECT().handlePacket(gomock.Any())
runner.onHandshakeComplete(sess)
@ -187,8 +187,8 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
sess := NewMockPacketHandler(mockCtrl)
) (quicSession, error) {
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any()).Do(func(_ *receivedPacket) { close(handledPacket) })
sess.EXPECT().run().Return(testErr)
return sess, nil
@ -202,7 +202,7 @@ var _ = Describe("Client", func() {
It("closes the session when the context is canceledd", func() {
sessionRunning := make(chan struct{})
defer close(sessionRunning)
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run().Do(func() {
<-sessionRunning
})
@ -217,7 +217,7 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
return sess, nil
}
ctx, cancel := context.WithCancel(context.Background())
@ -300,7 +300,7 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
return nil, testErr
}
_, err := Dial(packetConn, addr, "quic.clemente.io:1337", nil, nil)
@ -328,14 +328,14 @@ var _ = Describe("Client", func() {
paramsChan <-chan handshake.TransportParameters,
_ protocol.PacketNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
cconn = connP
hostname = hostnameP
version = versionP
conf = configP
close(c)
// TODO: check connection IDs?
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run()
return sess, nil
}
@ -374,9 +374,9 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
Expect(conn.Write([]byte("0 fake CHLO"))).To(Succeed())
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run().Return(testErr)
return sess, nil
}
@ -385,7 +385,7 @@ var _ = Describe("Client", func() {
})
It("recognizes that a packet without VersionFlag means that the server accepted the suggested version", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any())
cl.session = sess
ph := wire.Header{
@ -406,13 +406,13 @@ var _ = Describe("Client", func() {
version1 := protocol.Version39
version2 := protocol.Version39 + 1
Expect(version2.UsesTLS()).To(BeFalse())
sess1 := NewMockPacketHandler(mockCtrl)
sess1 := NewMockQuicSession(mockCtrl)
run1 := make(chan struct{})
sess1.EXPECT().run().Do(func() { <-run1 }).Return(errCloseSessionForNewVersion)
sess1.EXPECT().Close(errCloseSessionForNewVersion).Do(func(error) { close(run1) })
sess2 := NewMockPacketHandler(mockCtrl)
sess2 := NewMockQuicSession(mockCtrl)
sess2.EXPECT().run()
sessionChan := make(chan *MockPacketHandler, 2)
sessionChan := make(chan *MockQuicSession, 2)
sessionChan <- sess1
sessionChan <- sess2
newClientSession = func(
@ -426,7 +426,7 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
return <-sessionChan, nil
}
@ -450,13 +450,13 @@ var _ = Describe("Client", func() {
version3 := protocol.Version39 + 2
Expect(version2.UsesTLS()).To(BeFalse())
Expect(version3.UsesTLS()).To(BeFalse())
sess1 := NewMockPacketHandler(mockCtrl)
sess1 := NewMockQuicSession(mockCtrl)
run1 := make(chan struct{})
sess1.EXPECT().run().Do(func() { <-run1 }).Return(errCloseSessionForNewVersion)
sess1.EXPECT().Close(errCloseSessionForNewVersion).Do(func(error) { close(run1) })
sess2 := NewMockPacketHandler(mockCtrl)
sess2 := NewMockQuicSession(mockCtrl)
sess2.EXPECT().run()
sessionChan := make(chan *MockPacketHandler, 2)
sessionChan := make(chan *MockQuicSession, 2)
sessionChan <- sess1
sessionChan <- sess2
newClientSession = func(
@ -470,7 +470,7 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
return <-sessionChan, nil
}
@ -492,7 +492,7 @@ var _ = Describe("Client", func() {
})
It("errors if no matching version is found", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().Close(gomock.Any())
cl.session = sess
cl.config = &Config{Versions: protocol.SupportedVersions}
@ -501,7 +501,7 @@ var _ = Describe("Client", func() {
})
It("errors if the version is supported by quic-go, but disabled by the quic.Config", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().Close(gomock.Any())
cl.session = sess
v := protocol.VersionNumber(1234)
@ -512,7 +512,7 @@ var _ = Describe("Client", func() {
})
It("changes to the version preferred by the quic.Config", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().Close(errCloseSessionForNewVersion)
cl.session = sess
config := &Config{Versions: []protocol.VersionNumber{1234, 4321}}
@ -532,14 +532,14 @@ var _ = Describe("Client", func() {
})
It("ignores packets with an invalid public header", func() {
cl.session = NewMockPacketHandler(mockCtrl) // don't EXPECT any handlePacket calls
cl.session = NewMockQuicSession(mockCtrl) // don't EXPECT any handlePacket calls
err := cl.handlePacket(addr, []byte("invalid packet"))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error parsing packet from"))
})
It("errors on packets that are smaller than the Payload Length in the packet header", func() {
cl.session = NewMockPacketHandler(mockCtrl) // don't EXPECT any handlePacket calls
cl.session = NewMockQuicSession(mockCtrl) // don't EXPECT any handlePacket calls
b := &bytes.Buffer{}
hdr := &wire.Header{
IsLongHeader: true,
@ -555,7 +555,7 @@ var _ = Describe("Client", func() {
})
It("cuts packets at the payload length", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any()).Do(func(packet *receivedPacket) {
Expect(packet.data).To(HaveLen(123))
})
@ -592,7 +592,7 @@ var _ = Describe("Client", func() {
})
It("ignores packets without connection id, if it didn't request connection id trunctation", func() {
cl.session = NewMockPacketHandler(mockCtrl) // don't EXPECT any handlePacket calls
cl.session = NewMockQuicSession(mockCtrl) // don't EXPECT any handlePacket calls
cl.config = &Config{RequestConnectionIDOmission: false}
buf := &bytes.Buffer{}
err := (&wire.Header{
@ -608,7 +608,7 @@ var _ = Describe("Client", func() {
})
It("ignores packets with the wrong destination connection ID", func() {
cl.session = NewMockPacketHandler(mockCtrl) // don't EXPECT any handlePacket calls
cl.session = NewMockQuicSession(mockCtrl) // don't EXPECT any handlePacket calls
buf := &bytes.Buffer{}
cl.version = versionIETFFrames
cl.config = &Config{RequestConnectionIDOmission: false}
@ -644,13 +644,13 @@ var _ = Describe("Client", func() {
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
cconn = connP
hostname = hostnameP
version = versionP
conf = configP
close(c)
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().run()
return sess, nil
}
@ -666,11 +666,11 @@ var _ = Describe("Client", func() {
It("creates a new session when the server performs a retry", func() {
config := &Config{Versions: []protocol.VersionNumber{protocol.VersionTLS}}
cl.config = config
sess1 := NewMockPacketHandler(mockCtrl)
sess1 := NewMockQuicSession(mockCtrl)
sess1.EXPECT().run().Return(handshake.ErrCloseSessionForRetry)
sess2 := NewMockPacketHandler(mockCtrl)
sess2 := NewMockQuicSession(mockCtrl)
sess2.EXPECT().run()
sessions := []*MockPacketHandler{sess1, sess2}
sessions := []*MockQuicSession{sess1, sess2}
newTLSClientSession = func(
connP connection,
_ sessionRunner,
@ -683,7 +683,7 @@ var _ = Describe("Client", func() {
paramsChan <-chan handshake.TransportParameters,
_ protocol.PacketNumber,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
sess := sessions[0]
sessions = sessions[1:]
return sess, nil
@ -749,7 +749,7 @@ var _ = Describe("Client", func() {
Context("handling packets", func() {
It("handles packets", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any())
cl.session = sess
ph := wire.Header{
@ -780,7 +780,7 @@ var _ = Describe("Client", func() {
It("closes the session when encountering an error while reading from the connection", func() {
testErr := errors.New("test error")
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().Close(testErr)
cl.session = sess
packetConn.readErr = testErr
@ -790,7 +790,7 @@ var _ = Describe("Client", func() {
Context("Public Reset handling", func() {
It("closes the session when receiving a Public Reset", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().closeRemote(gomock.Any()).Do(func(err error) {
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.PublicReset))
})
@ -800,14 +800,14 @@ var _ = Describe("Client", func() {
})
It("ignores Public Resets from the wrong remote address", func() {
cl.session = NewMockPacketHandler(mockCtrl) // don't EXPECT any calls
cl.session = NewMockQuicSession(mockCtrl) // don't EXPECT any calls
spoofedAddr := &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 5678}
err := cl.handlePacket(spoofedAddr, wire.WritePublicReset(cl.destConnID, 1, 0))
Expect(err).To(MatchError("Received a spoofed Public Reset"))
})
It("ignores unparseable Public Resets", func() {
cl.session = NewMockPacketHandler(mockCtrl) // don't EXPECT any calls
cl.session = NewMockQuicSession(mockCtrl) // don't EXPECT any calls
pr := wire.WritePublicReset(cl.destConnID, 1, 0)
err := cl.handlePacket(addr, pr[:len(pr)-5])
Expect(err).To(HaveOccurred())

View file

@ -1,5 +1,5 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/lucas-clemente/quic-go (interfaces: PacketHandler)
// Source: github.com/lucas-clemente/quic-go (interfaces: QuicSession)
// Package quic is a generated GoMock package.
package quic
@ -14,31 +14,31 @@ import (
protocol "github.com/lucas-clemente/quic-go/internal/protocol"
)
// MockPacketHandler is a mock of PacketHandler interface
type MockPacketHandler struct {
// MockQuicSession is a mock of QuicSession interface
type MockQuicSession struct {
ctrl *gomock.Controller
recorder *MockPacketHandlerMockRecorder
recorder *MockQuicSessionMockRecorder
}
// MockPacketHandlerMockRecorder is the mock recorder for MockPacketHandler
type MockPacketHandlerMockRecorder struct {
mock *MockPacketHandler
// MockQuicSessionMockRecorder is the mock recorder for MockQuicSession
type MockQuicSessionMockRecorder struct {
mock *MockQuicSession
}
// NewMockPacketHandler creates a new mock instance
func NewMockPacketHandler(ctrl *gomock.Controller) *MockPacketHandler {
mock := &MockPacketHandler{ctrl: ctrl}
mock.recorder = &MockPacketHandlerMockRecorder{mock}
// NewMockQuicSession creates a new mock instance
func NewMockQuicSession(ctrl *gomock.Controller) *MockQuicSession {
mock := &MockQuicSession{ctrl: ctrl}
mock.recorder = &MockQuicSessionMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockPacketHandler) EXPECT() *MockPacketHandlerMockRecorder {
func (m *MockQuicSession) EXPECT() *MockQuicSessionMockRecorder {
return m.recorder
}
// AcceptStream mocks base method
func (m *MockPacketHandler) AcceptStream() (Stream, error) {
func (m *MockQuicSession) AcceptStream() (Stream, error) {
ret := m.ctrl.Call(m, "AcceptStream")
ret0, _ := ret[0].(Stream)
ret1, _ := ret[1].(error)
@ -46,12 +46,12 @@ func (m *MockPacketHandler) AcceptStream() (Stream, error) {
}
// AcceptStream indicates an expected call of AcceptStream
func (mr *MockPacketHandlerMockRecorder) AcceptStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AcceptStream", reflect.TypeOf((*MockPacketHandler)(nil).AcceptStream))
func (mr *MockQuicSessionMockRecorder) AcceptStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AcceptStream", reflect.TypeOf((*MockQuicSession)(nil).AcceptStream))
}
// AcceptUniStream mocks base method
func (m *MockPacketHandler) AcceptUniStream() (ReceiveStream, error) {
func (m *MockQuicSession) AcceptUniStream() (ReceiveStream, error) {
ret := m.ctrl.Call(m, "AcceptUniStream")
ret0, _ := ret[0].(ReceiveStream)
ret1, _ := ret[1].(error)
@ -59,72 +59,72 @@ func (m *MockPacketHandler) AcceptUniStream() (ReceiveStream, error) {
}
// AcceptUniStream indicates an expected call of AcceptUniStream
func (mr *MockPacketHandlerMockRecorder) AcceptUniStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AcceptUniStream", reflect.TypeOf((*MockPacketHandler)(nil).AcceptUniStream))
func (mr *MockQuicSessionMockRecorder) AcceptUniStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AcceptUniStream", reflect.TypeOf((*MockQuicSession)(nil).AcceptUniStream))
}
// Close mocks base method
func (m *MockPacketHandler) Close(arg0 error) error {
func (m *MockQuicSession) Close(arg0 error) error {
ret := m.ctrl.Call(m, "Close", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// Close indicates an expected call of Close
func (mr *MockPacketHandlerMockRecorder) Close(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockPacketHandler)(nil).Close), arg0)
func (mr *MockQuicSessionMockRecorder) Close(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockQuicSession)(nil).Close), arg0)
}
// ConnectionState mocks base method
func (m *MockPacketHandler) ConnectionState() handshake.ConnectionState {
func (m *MockQuicSession) ConnectionState() handshake.ConnectionState {
ret := m.ctrl.Call(m, "ConnectionState")
ret0, _ := ret[0].(handshake.ConnectionState)
return ret0
}
// ConnectionState indicates an expected call of ConnectionState
func (mr *MockPacketHandlerMockRecorder) ConnectionState() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnectionState", reflect.TypeOf((*MockPacketHandler)(nil).ConnectionState))
func (mr *MockQuicSessionMockRecorder) ConnectionState() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnectionState", reflect.TypeOf((*MockQuicSession)(nil).ConnectionState))
}
// Context mocks base method
func (m *MockPacketHandler) Context() context.Context {
func (m *MockQuicSession) Context() context.Context {
ret := m.ctrl.Call(m, "Context")
ret0, _ := ret[0].(context.Context)
return ret0
}
// Context indicates an expected call of Context
func (mr *MockPacketHandlerMockRecorder) Context() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockPacketHandler)(nil).Context))
func (mr *MockQuicSessionMockRecorder) Context() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockQuicSession)(nil).Context))
}
// GetVersion mocks base method
func (m *MockPacketHandler) GetVersion() protocol.VersionNumber {
func (m *MockQuicSession) GetVersion() protocol.VersionNumber {
ret := m.ctrl.Call(m, "GetVersion")
ret0, _ := ret[0].(protocol.VersionNumber)
return ret0
}
// GetVersion indicates an expected call of GetVersion
func (mr *MockPacketHandlerMockRecorder) GetVersion() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVersion", reflect.TypeOf((*MockPacketHandler)(nil).GetVersion))
func (mr *MockQuicSessionMockRecorder) GetVersion() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVersion", reflect.TypeOf((*MockQuicSession)(nil).GetVersion))
}
// LocalAddr mocks base method
func (m *MockPacketHandler) LocalAddr() net.Addr {
func (m *MockQuicSession) LocalAddr() net.Addr {
ret := m.ctrl.Call(m, "LocalAddr")
ret0, _ := ret[0].(net.Addr)
return ret0
}
// LocalAddr indicates an expected call of LocalAddr
func (mr *MockPacketHandlerMockRecorder) LocalAddr() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalAddr", reflect.TypeOf((*MockPacketHandler)(nil).LocalAddr))
func (mr *MockQuicSessionMockRecorder) LocalAddr() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalAddr", reflect.TypeOf((*MockQuicSession)(nil).LocalAddr))
}
// OpenStream mocks base method
func (m *MockPacketHandler) OpenStream() (Stream, error) {
func (m *MockQuicSession) OpenStream() (Stream, error) {
ret := m.ctrl.Call(m, "OpenStream")
ret0, _ := ret[0].(Stream)
ret1, _ := ret[1].(error)
@ -132,12 +132,12 @@ func (m *MockPacketHandler) OpenStream() (Stream, error) {
}
// OpenStream indicates an expected call of OpenStream
func (mr *MockPacketHandlerMockRecorder) OpenStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenStream", reflect.TypeOf((*MockPacketHandler)(nil).OpenStream))
func (mr *MockQuicSessionMockRecorder) OpenStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenStream", reflect.TypeOf((*MockQuicSession)(nil).OpenStream))
}
// OpenStreamSync mocks base method
func (m *MockPacketHandler) OpenStreamSync() (Stream, error) {
func (m *MockQuicSession) OpenStreamSync() (Stream, error) {
ret := m.ctrl.Call(m, "OpenStreamSync")
ret0, _ := ret[0].(Stream)
ret1, _ := ret[1].(error)
@ -145,12 +145,12 @@ func (m *MockPacketHandler) OpenStreamSync() (Stream, error) {
}
// OpenStreamSync indicates an expected call of OpenStreamSync
func (mr *MockPacketHandlerMockRecorder) OpenStreamSync() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenStreamSync", reflect.TypeOf((*MockPacketHandler)(nil).OpenStreamSync))
func (mr *MockQuicSessionMockRecorder) OpenStreamSync() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenStreamSync", reflect.TypeOf((*MockQuicSession)(nil).OpenStreamSync))
}
// OpenUniStream mocks base method
func (m *MockPacketHandler) OpenUniStream() (SendStream, error) {
func (m *MockQuicSession) OpenUniStream() (SendStream, error) {
ret := m.ctrl.Call(m, "OpenUniStream")
ret0, _ := ret[0].(SendStream)
ret1, _ := ret[1].(error)
@ -158,12 +158,12 @@ func (m *MockPacketHandler) OpenUniStream() (SendStream, error) {
}
// OpenUniStream indicates an expected call of OpenUniStream
func (mr *MockPacketHandlerMockRecorder) OpenUniStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenUniStream", reflect.TypeOf((*MockPacketHandler)(nil).OpenUniStream))
func (mr *MockQuicSessionMockRecorder) OpenUniStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenUniStream", reflect.TypeOf((*MockQuicSession)(nil).OpenUniStream))
}
// OpenUniStreamSync mocks base method
func (m *MockPacketHandler) OpenUniStreamSync() (SendStream, error) {
func (m *MockQuicSession) OpenUniStreamSync() (SendStream, error) {
ret := m.ctrl.Call(m, "OpenUniStreamSync")
ret0, _ := ret[0].(SendStream)
ret1, _ := ret[1].(error)
@ -171,62 +171,62 @@ func (m *MockPacketHandler) OpenUniStreamSync() (SendStream, error) {
}
// OpenUniStreamSync indicates an expected call of OpenUniStreamSync
func (mr *MockPacketHandlerMockRecorder) OpenUniStreamSync() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenUniStreamSync", reflect.TypeOf((*MockPacketHandler)(nil).OpenUniStreamSync))
func (mr *MockQuicSessionMockRecorder) OpenUniStreamSync() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenUniStreamSync", reflect.TypeOf((*MockQuicSession)(nil).OpenUniStreamSync))
}
// RemoteAddr mocks base method
func (m *MockPacketHandler) RemoteAddr() net.Addr {
func (m *MockQuicSession) RemoteAddr() net.Addr {
ret := m.ctrl.Call(m, "RemoteAddr")
ret0, _ := ret[0].(net.Addr)
return ret0
}
// RemoteAddr indicates an expected call of RemoteAddr
func (mr *MockPacketHandlerMockRecorder) RemoteAddr() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoteAddr", reflect.TypeOf((*MockPacketHandler)(nil).RemoteAddr))
func (mr *MockQuicSessionMockRecorder) RemoteAddr() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoteAddr", reflect.TypeOf((*MockQuicSession)(nil).RemoteAddr))
}
// closeRemote mocks base method
func (m *MockPacketHandler) closeRemote(arg0 error) {
func (m *MockQuicSession) closeRemote(arg0 error) {
m.ctrl.Call(m, "closeRemote", arg0)
}
// closeRemote indicates an expected call of closeRemote
func (mr *MockPacketHandlerMockRecorder) closeRemote(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "closeRemote", reflect.TypeOf((*MockPacketHandler)(nil).closeRemote), arg0)
func (mr *MockQuicSessionMockRecorder) closeRemote(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "closeRemote", reflect.TypeOf((*MockQuicSession)(nil).closeRemote), arg0)
}
// getCryptoStream mocks base method
func (m *MockPacketHandler) getCryptoStream() cryptoStreamI {
func (m *MockQuicSession) getCryptoStream() cryptoStreamI {
ret := m.ctrl.Call(m, "getCryptoStream")
ret0, _ := ret[0].(cryptoStreamI)
return ret0
}
// getCryptoStream indicates an expected call of getCryptoStream
func (mr *MockPacketHandlerMockRecorder) getCryptoStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "getCryptoStream", reflect.TypeOf((*MockPacketHandler)(nil).getCryptoStream))
func (mr *MockQuicSessionMockRecorder) getCryptoStream() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "getCryptoStream", reflect.TypeOf((*MockQuicSession)(nil).getCryptoStream))
}
// handlePacket mocks base method
func (m *MockPacketHandler) handlePacket(arg0 *receivedPacket) {
func (m *MockQuicSession) handlePacket(arg0 *receivedPacket) {
m.ctrl.Call(m, "handlePacket", arg0)
}
// handlePacket indicates an expected call of handlePacket
func (mr *MockPacketHandlerMockRecorder) handlePacket(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "handlePacket", reflect.TypeOf((*MockPacketHandler)(nil).handlePacket), arg0)
func (mr *MockQuicSessionMockRecorder) handlePacket(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "handlePacket", reflect.TypeOf((*MockQuicSession)(nil).handlePacket), arg0)
}
// run mocks base method
func (m *MockPacketHandler) run() error {
func (m *MockQuicSession) run() error {
ret := m.ctrl.Call(m, "run")
ret0, _ := ret[0].(error)
return ret0
}
// run indicates an expected call of run
func (mr *MockPacketHandlerMockRecorder) run() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "run", reflect.TypeOf((*MockPacketHandler)(nil).run))
func (mr *MockQuicSessionMockRecorder) run() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "run", reflect.TypeOf((*MockQuicSession)(nil).run))
}

View file

@ -35,7 +35,7 @@ func (m *MockSessionRunner) EXPECT() *MockSessionRunnerMockRecorder {
}
// onHandshakeComplete mocks base method
func (m *MockSessionRunner) onHandshakeComplete(arg0 packetHandler) {
func (m *MockSessionRunner) onHandshakeComplete(arg0 Session) {
m.ctrl.Call(m, "onHandshakeComplete", arg0)
}

View file

@ -12,7 +12,7 @@ package quic
//go:generate sh -c "./mockgen_private.sh quic mock_quic_aead_test.go github.com/lucas-clemente/quic-go quicAEAD QuicAEAD"
//go:generate sh -c "./mockgen_private.sh quic mock_gquic_aead_test.go github.com/lucas-clemente/quic-go gQUICAEAD GQUICAEAD"
//go:generate sh -c "./mockgen_private.sh quic mock_session_runner_test.go github.com/lucas-clemente/quic-go sessionRunner SessionRunner"
//go:generate sh -c "./mockgen_private.sh quic mock_packet_handler_test.go github.com/lucas-clemente/quic-go packetHandler PacketHandler"
//go:generate sh -c "./mockgen_private.sh quic mock_quic_session_test.go github.com/lucas-clemente/quic-go quicSession QuicSession"
//go:generate sh -c "./mockgen_private.sh quic mock_session_handler_test.go github.com/lucas-clemente/quic-go sessionHandler SessionHandler"
//go:generate sh -c "find . -type f -name 'mock_*_test.go' | xargs sed -i '' 's/quic_go.//g'"
//go:generate sh -c "goimports -w mock*_test.go"

View file

@ -18,25 +18,30 @@ import (
// packetHandler handles packets
type packetHandler interface {
Session
getCryptoStream() cryptoStreamI
handlePacket(*receivedPacket)
Close(error) error
}
type quicSession interface {
Session
handlePacket(*receivedPacket)
getCryptoStream() cryptoStreamI
GetVersion() protocol.VersionNumber
run() error
closeRemote(error)
}
type sessionRunner interface {
onHandshakeComplete(packetHandler)
onHandshakeComplete(Session)
removeConnectionID(protocol.ConnectionID)
}
type runner struct {
onHandshakeCompleteImpl func(packetHandler)
onHandshakeCompleteImpl func(Session)
removeConnectionIDImpl func(protocol.ConnectionID)
}
func (r *runner) onHandshakeComplete(p packetHandler) { r.onHandshakeCompleteImpl(p) }
func (r *runner) onHandshakeComplete(s Session) { r.onHandshakeCompleteImpl(s) }
func (r *runner) removeConnectionID(c protocol.ConnectionID) { r.removeConnectionIDImpl(c) }
var _ sessionRunner = &runner{}
@ -70,7 +75,7 @@ type server struct {
sessionRunner sessionRunner
// set as a member, so they can be set in the tests
newSession func(connection, sessionRunner, protocol.VersionNumber, protocol.ConnectionID, *handshake.ServerConfig, *tls.Config, *Config, utils.Logger) (packetHandler, error)
newSession func(connection, sessionRunner, protocol.VersionNumber, protocol.ConnectionID, *handshake.ServerConfig, *tls.Config, *Config, utils.Logger) (quicSession, error)
logger utils.Logger
}
@ -143,7 +148,7 @@ func Listen(conn net.PacketConn, tlsConf *tls.Config, config *Config) (Listener,
func (s *server) setup() {
s.sessionRunner = &runner{
onHandshakeCompleteImpl: func(sess packetHandler) { s.sessionQueue <- sess },
onHandshakeCompleteImpl: func(sess Session) { s.sessionQueue <- sess },
removeConnectionIDImpl: s.sessionHandler.Remove,
}
}
@ -405,8 +410,7 @@ func (s *server) handleGQUICPacket(hdr *wire.Header, packetData []byte, remoteAd
}
s.logger.Infof("Serving new connection: %s, version %s from %v", hdr.DestConnectionID, version, remoteAddr)
var err error
session, err = s.newSession(
sess, err := s.newSession(
&conn{pconn: s.conn, currentAddr: remoteAddr},
s.sessionRunner,
version,
@ -419,9 +423,10 @@ func (s *server) handleGQUICPacket(hdr *wire.Header, packetData []byte, remoteAd
if err != nil {
return err
}
s.sessionHandler.Add(hdr.DestConnectionID, session)
s.sessionHandler.Add(hdr.DestConnectionID, sess)
go session.run()
go sess.run()
session = sess
}
session.handlePacket(&receivedPacket{

View file

@ -21,7 +21,7 @@ import (
)
type mockSession struct {
*MockPacketHandler
*MockQuicSession
connID protocol.ConnectionID
runner sessionRunner
@ -83,7 +83,7 @@ var _ = Describe("Server", func() {
serv *server
firstPacket []byte // a valid first packet for a new connection with connectionID 0x4cfa9f9b668619f6 (= connID)
connID = protocol.ConnectionID{0x4c, 0xfa, 0x9f, 0x9b, 0x66, 0x86, 0x19, 0xf6}
sessions = make([]*MockPacketHandler, 0)
sessions = make([]*MockQuicSession, 0)
sessionHandler *MockSessionHandler
)
@ -98,9 +98,9 @@ var _ = Describe("Server", func() {
_ *tls.Config,
_ *Config,
_ utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
ExpectWithOffset(0, sessions).ToNot(BeEmpty())
s := &mockSession{MockPacketHandler: sessions[0]}
s := &mockSession{MockQuicSession: sessions[0]}
s.connID = connID
s.runner = runner
sessions = sessions[1:]
@ -136,7 +136,7 @@ var _ = Describe("Server", func() {
})
It("creates new sessions", func() {
s := NewMockPacketHandler(mockCtrl)
s := NewMockQuicSession(mockCtrl)
s.EXPECT().handlePacket(gomock.Any())
run := make(chan struct{})
s.EXPECT().run().Do(func() { close(run) })
@ -153,7 +153,7 @@ var _ = Describe("Server", func() {
It("accepts new TLS sessions", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
err := serv.setupTLS()
Expect(err).ToNot(HaveOccurred())
added := make(chan struct{})
@ -168,7 +168,7 @@ var _ = Describe("Server", func() {
})
It("accepts a session once the connection it is forward secure", func() {
s := NewMockPacketHandler(mockCtrl)
s := NewMockQuicSession(mockCtrl)
s.EXPECT().handlePacket(gomock.Any())
run := make(chan struct{})
s.EXPECT().run().Do(func() { close(run) })
@ -184,7 +184,7 @@ var _ = Describe("Server", func() {
sessionHandler.EXPECT().Get(connID)
sessionHandler.EXPECT().Add(connID, gomock.Any()).Do(func(_ protocol.ConnectionID, sess packetHandler) {
Consistently(done).ShouldNot(BeClosed())
sess.(*mockSession).runner.onHandshakeComplete(sess)
sess.(*mockSession).runner.onHandshakeComplete(sess.(Session))
})
err := serv.handlePacket(nil, firstPacket)
Expect(err).ToNot(HaveOccurred())
@ -194,7 +194,7 @@ var _ = Describe("Server", func() {
It("doesn't accept sessions that error during the handshake", func() {
run := make(chan error, 1)
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any())
sess.EXPECT().run().DoAndReturn(func() error { return <-run })
sessions = append(sessions, sess)
@ -219,7 +219,7 @@ var _ = Describe("Server", func() {
})
It("assigns packets to existing sessions", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any())
sessionHandler.EXPECT().Get(connID).Return(sess, true)
@ -283,7 +283,7 @@ var _ = Describe("Server", func() {
})
It("ignores delayed packets with mismatching versions", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
// don't EXPECT any handlePacket() calls to this session
sessionHandler.EXPECT().Get(connID).Return(sess, true)
@ -321,7 +321,7 @@ var _ = Describe("Server", func() {
})
It("cuts packets at the payload length", func() {
sess := NewMockPacketHandler(mockCtrl)
sess := NewMockQuicSession(mockCtrl)
sess.EXPECT().handlePacket(gomock.Any()).Do(func(packet *receivedPacket) {
Expect(packet.data).To(HaveLen(123))
})

View file

@ -42,7 +42,7 @@ type serverTLS struct {
params *handshake.TransportParameters
newMintConn func(*handshake.CryptoStreamConn, protocol.VersionNumber) (handshake.MintTLS, <-chan handshake.TransportParameters, error)
newSession func(connection, sessionRunner, protocol.ConnectionID, protocol.ConnectionID, protocol.PacketNumber, *Config, handshake.MintTLS, *handshake.CryptoStreamConn, crypto.AEAD, *handshake.TransportParameters, protocol.VersionNumber, utils.Logger) (packetHandler, error)
newSession func(connection, sessionRunner, protocol.ConnectionID, protocol.ConnectionID, protocol.PacketNumber, *Config, handshake.MintTLS, *handshake.CryptoStreamConn, crypto.AEAD, *handshake.TransportParameters, protocol.VersionNumber, utils.Logger) (quicSession, error)
sessionRunner sessionRunner
sessionChan chan<- tlsSession

View file

@ -145,8 +145,8 @@ var _ = Describe("Stateless TLS handling", func() {
mintReply.Write([]byte("Server Hello"))
})
run := make(chan struct{})
server.newSession = func(connection, sessionRunner, protocol.ConnectionID, protocol.ConnectionID, protocol.PacketNumber, *Config, handshake.MintTLS, *handshake.CryptoStreamConn, crypto.AEAD, *handshake.TransportParameters, protocol.VersionNumber, utils.Logger) (packetHandler, error) {
sess := NewMockPacketHandler(mockCtrl)
server.newSession = func(connection, sessionRunner, protocol.ConnectionID, protocol.ConnectionID, protocol.PacketNumber, *Config, handshake.MintTLS, *handshake.CryptoStreamConn, crypto.AEAD, *handshake.TransportParameters, protocol.VersionNumber, utils.Logger) (quicSession, error) {
sess := NewMockQuicSession(mockCtrl)
cryptoStream := NewMockCryptoStream(mockCtrl)
cryptoStream.EXPECT().setReadOffset(gomock.Any())
sess.EXPECT().getCryptoStream().Return(cryptoStream)

View file

@ -156,7 +156,7 @@ func newSession(
tlsConf *tls.Config,
config *Config,
logger utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
paramsChan := make(chan handshake.TransportParameters)
handshakeEvent := make(chan struct{}, 1)
s := &session{
@ -230,7 +230,7 @@ var newClientSession = func(
initialVersion protocol.VersionNumber,
negotiatedVersions []protocol.VersionNumber, // needed for validation of the GQUIC version negotiation
logger utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
paramsChan := make(chan handshake.TransportParameters)
handshakeEvent := make(chan struct{}, 1)
s := &session{
@ -301,7 +301,7 @@ func newTLSServerSession(
peerParams *handshake.TransportParameters,
v protocol.VersionNumber,
logger utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
handshakeEvent := make(chan struct{}, 1)
s := &session{
conn: conn,
@ -359,7 +359,7 @@ var newTLSClientSession = func(
paramsChan <-chan handshake.TransportParameters,
initialPacketNumber protocol.PacketNumber,
logger utils.Logger,
) (packetHandler, error) {
) (quicSession, error) {
handshakeEvent := make(chan struct{}, 1)
s := &session{
conn: conn,

View file

@ -45,9 +45,9 @@ var _ = Describe("Session Handler", func() {
})
It("closes", func() {
sess1 := NewMockPacketHandler(mockCtrl)
sess1 := NewMockQuicSession(mockCtrl)
sess1.EXPECT().Close(nil)
sess2 := NewMockPacketHandler(mockCtrl)
sess2 := NewMockQuicSession(mockCtrl)
sess2.EXPECT().Close(nil)
handler.Add(protocol.ConnectionID{1, 1, 1, 1}, sess1)
handler.Add(protocol.ConnectionID{2, 2, 2, 2}, sess2)