From c02f90447990972ebaa498fe80b36e0532586c1c Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Fri, 9 Jun 2017 12:38:07 +0200 Subject: [PATCH] Mock CPM everywhere using gomock --- packet_packer_test.go | 9 ++++++--- session_test.go | 23 ++++++++++++++++------- stream_framer_test.go | 2 +- streams_map_test.go | 42 ------------------------------------------ 4 files changed, 23 insertions(+), 53 deletions(-) diff --git a/packet_packer_test.go b/packet_packer_test.go index 83dfa573..f0369cd9 100644 --- a/packet_packer_test.go +++ b/packet_packer_test.go @@ -6,6 +6,7 @@ import ( "github.com/lucas-clemente/quic-go/ackhandler" "github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/handshake" + "github.com/lucas-clemente/quic-go/internal/mocks" "github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/qerr" . "github.com/onsi/ginkgo" @@ -48,12 +49,14 @@ var _ = Describe("Packet packer", func() { ) BeforeEach(func() { - cpm := &mockConnectionParametersManager{} - streamFramer = newStreamFramer(newStreamsMap(nil, protocol.PerspectiveServer, cpm), nil) + mockCpm := mocks.NewMockConnectionParametersManager(mockCtrl) + mockCpm.EXPECT().TruncateConnectionID().Return(false).AnyTimes() + + streamFramer = newStreamFramer(newStreamsMap(nil, protocol.PerspectiveServer, nil), nil) packer = &packetPacker{ cryptoSetup: &mockCryptoSetup{encLevelSeal: protocol.EncryptionForwardSecure}, - connectionParameters: cpm, + connectionParameters: mockCpm, connectionID: 0x1337, packetNumberGenerator: newPacketNumberGenerator(protocol.SkipPacketAveragePeriodLength), streamFramer: streamFramer, diff --git a/session_test.go b/session_test.go index 9fd16369..b32786b5 100644 --- a/session_test.go +++ b/session_test.go @@ -17,6 +17,7 @@ import ( "github.com/lucas-clemente/quic-go/crypto" "github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/handshake" + "github.com/lucas-clemente/quic-go/internal/mocks" "github.com/lucas-clemente/quic-go/internal/mocks/mocks_fc" "github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/qerr" @@ -127,7 +128,7 @@ var _ = Describe("Session", func() { sess *session scfg *handshake.ServerConfig mconn *mockConnection - cpm *mockConnectionParametersManager + mockCpm *mocks.MockConnectionParametersManager cryptoSetup *mockCryptoSetup handshakeChan <-chan handshakeEvent aeadChanged chan<- protocol.EncryptionLevel @@ -172,8 +173,9 @@ var _ = Describe("Session", func() { sess = pSess.(*session) Expect(sess.streamsMap.openStreams).To(HaveLen(1)) // Crypto stream - cpm = &mockConnectionParametersManager{idleTime: 60 * time.Second} - sess.connectionParameters = cpm + mockCpm = mocks.NewMockConnectionParametersManager(mockCtrl) + mockCpm.EXPECT().GetIdleConnectionStateLifetime().Return(time.Minute).AnyTimes() + sess.connectionParameters = mockCpm }) AfterEach(func() { @@ -1417,8 +1419,11 @@ var _ = Describe("Session", func() { It("does not use ICSL before handshake", func(done Done) { sess.lastNetworkActivityTime = time.Now().Add(-time.Minute) - cpm.idleTime = 99999 * time.Second - sess.packer.connectionParameters = sess.connectionParameters + mockCpm = mocks.NewMockConnectionParametersManager(mockCtrl) + mockCpm.EXPECT().GetIdleConnectionStateLifetime().Return(9999 * time.Second).AnyTimes() + mockCpm.EXPECT().TruncateConnectionID().Return(false).AnyTimes() + sess.connectionParameters = mockCpm + sess.packer.connectionParameters = mockCpm err := sess.run() // Would normally not return Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.NetworkIdleTimeout)) Expect(mconn.written[0]).To(ContainSubstring("No recent network activity.")) @@ -1428,8 +1433,12 @@ var _ = Describe("Session", func() { It("uses ICSL after handshake", func(done Done) { close(aeadChanged) - cpm.idleTime = 0 * time.Millisecond - sess.packer.connectionParameters = sess.connectionParameters + mockCpm = mocks.NewMockConnectionParametersManager(mockCtrl) + mockCpm.EXPECT().GetIdleConnectionStateLifetime().Return(0 * time.Second) + mockCpm.EXPECT().TruncateConnectionID().Return(false).AnyTimes() + sess.connectionParameters = mockCpm + sess.packer.connectionParameters = mockCpm + mockCpm.EXPECT().GetIdleConnectionStateLifetime().Return(0 * time.Second).AnyTimes() err := sess.run() // Would normally not return Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.NetworkIdleTimeout)) Expect(mconn.written[0]).To(ContainSubstring("No recent network activity.")) diff --git a/stream_framer_test.go b/stream_framer_test.go index cf7d7b32..506a544a 100644 --- a/stream_framer_test.go +++ b/stream_framer_test.go @@ -37,7 +37,7 @@ var _ = Describe("Stream Framer", func() { stream1 = &stream{streamID: id1} stream2 = &stream{streamID: id2} - streamsMap = newStreamsMap(nil, protocol.PerspectiveServer, &mockConnectionParametersManager{}) + streamsMap = newStreamsMap(nil, protocol.PerspectiveServer, nil) streamsMap.putStream(stream1) streamsMap.putStream(stream2) diff --git a/streams_map_test.go b/streams_map_test.go index b1697143..f73a75fc 100644 --- a/streams_map_test.go +++ b/streams_map_test.go @@ -2,10 +2,7 @@ package quic import ( "errors" - "math" - "time" - "github.com/lucas-clemente/quic-go/handshake" "github.com/lucas-clemente/quic-go/internal/mocks" "github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/qerr" @@ -13,45 +10,6 @@ import ( . "github.com/onsi/gomega" ) -type mockConnectionParametersManager struct { - maxIncomingStreams uint32 - maxOutgoingStreams uint32 - idleTime time.Duration -} - -func (m *mockConnectionParametersManager) SetFromMap(map[handshake.Tag][]byte) error { - panic("not implemented") -} -func (m *mockConnectionParametersManager) GetHelloMap() (map[handshake.Tag][]byte, error) { - panic("not implemented") -} -func (m *mockConnectionParametersManager) GetSendStreamFlowControlWindow() protocol.ByteCount { - return math.MaxUint64 -} -func (m *mockConnectionParametersManager) GetSendConnectionFlowControlWindow() protocol.ByteCount { - return math.MaxUint64 -} -func (m *mockConnectionParametersManager) GetReceiveStreamFlowControlWindow() protocol.ByteCount { - return math.MaxUint64 -} -func (m *mockConnectionParametersManager) GetMaxReceiveStreamFlowControlWindow() protocol.ByteCount { - return math.MaxUint64 -} -func (m *mockConnectionParametersManager) GetReceiveConnectionFlowControlWindow() protocol.ByteCount { - return math.MaxUint64 -} -func (m *mockConnectionParametersManager) GetMaxReceiveConnectionFlowControlWindow() protocol.ByteCount { - return math.MaxUint64 -} -func (m *mockConnectionParametersManager) GetMaxOutgoingStreams() uint32 { return m.maxOutgoingStreams } -func (m *mockConnectionParametersManager) GetMaxIncomingStreams() uint32 { return m.maxIncomingStreams } -func (m *mockConnectionParametersManager) GetIdleConnectionStateLifetime() time.Duration { - return m.idleTime -} -func (m *mockConnectionParametersManager) TruncateConnectionID() bool { return false } - -var _ handshake.ConnectionParametersManager = &mockConnectionParametersManager{} - var _ = Describe("Streams Map", func() { const ( maxIncomingStreams = 75