handshake: remove gomock tls.ClientSessionCache implementation (#4619)

This commit is contained in:
Marten Seemann
2024-08-03 16:43:30 -07:00
committed by GitHub
parent bb4eb8597c
commit 87f2894af5
3 changed files with 33 additions and 168 deletions

View File

@@ -11,7 +11,6 @@ import (
"net"
"time"
mocktls "github.com/quic-go/quic-go/internal/mocks/tls"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/internal/qerr"
"github.com/quic-go/quic-go/internal/testdata"
@@ -20,7 +19,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)
const (
@@ -28,6 +26,29 @@ const (
typeNewSessionTicket = 4
)
type mockClientSessionCache struct {
cache tls.ClientSessionCache
puts chan *tls.ClientSessionState
}
var _ tls.ClientSessionCache = &mockClientSessionCache{}
func newMockClientSessionCache() *mockClientSessionCache {
return &mockClientSessionCache{
puts: make(chan *tls.ClientSessionState, 1),
cache: tls.NewLRUClientSessionCache(1),
}
}
func (m *mockClientSessionCache) Get(sessionKey string) (session *tls.ClientSessionState, ok bool) {
return m.cache.Get(sessionKey)
}
func (m *mockClientSessionCache) Put(sessionKey string, cs *tls.ClientSessionState) {
m.puts <- cs
m.cache.Put(sessionKey, cs)
}
var _ = Describe("Crypto Setup TLS", func() {
generateCert := func() tls.Certificate {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
@@ -345,14 +366,7 @@ var _ = Describe("Crypto Setup TLS", func() {
})
It("uses session resumption", func() {
csc := mocktls.NewMockClientSessionCache(mockCtrl)
var state *tls.ClientSessionState
receivedSessionTicket := make(chan struct{})
csc.EXPECT().Get(gomock.Any())
csc.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(_ string, css *tls.ClientSessionState) {
state = css
close(receivedSessionTicket)
})
csc := newMockClientSessionCache()
clientConf.ClientSessionCache = csc
const serverRTT = 25 * time.Millisecond // RTT as measured by the server. Should be restored.
const clientRTT = 30 * time.Millisecond // RTT as measured by the client. Should be restored.
@@ -366,12 +380,10 @@ var _ = Describe("Crypto Setup TLS", func() {
)
Expect(clientErr).ToNot(HaveOccurred())
Expect(serverErr).ToNot(HaveOccurred())
Eventually(receivedSessionTicket).Should(BeClosed())
Eventually(csc.puts).Should(Receive())
Expect(server.ConnectionState().DidResume).To(BeFalse())
Expect(client.ConnectionState().DidResume).To(BeFalse())
csc.EXPECT().Get(gomock.Any()).Return(state, true)
csc.EXPECT().Put(gomock.Any(), gomock.Any()).MaxTimes(1)
clientRTTStats := &utils.RTTStats{}
serverRTTStats := &utils.RTTStats{}
client, _, clientErr, server, _, serverErr = handshakeWithTLSConf(
@@ -382,7 +394,7 @@ var _ = Describe("Crypto Setup TLS", func() {
)
Expect(clientErr).ToNot(HaveOccurred())
Expect(serverErr).ToNot(HaveOccurred())
Eventually(receivedSessionTicket).Should(BeClosed())
Eventually(csc.puts).Should(Receive())
Expect(server.ConnectionState().DidResume).To(BeTrue())
Expect(client.ConnectionState().DidResume).To(BeTrue())
Expect(clientRTTStats.SmoothedRTT()).To(Equal(clientRTT))
@@ -390,14 +402,7 @@ var _ = Describe("Crypto Setup TLS", func() {
})
It("doesn't use session resumption if the server disabled it", func() {
csc := mocktls.NewMockClientSessionCache(mockCtrl)
var state *tls.ClientSessionState
receivedSessionTicket := make(chan struct{})
csc.EXPECT().Get(gomock.Any())
csc.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(_ string, css *tls.ClientSessionState) {
state = css
close(receivedSessionTicket)
})
csc := newMockClientSessionCache()
clientConf.ClientSessionCache = csc
client, _, clientErr, server, _, serverErr := handshakeWithTLSConf(
clientConf, serverConf,
@@ -407,12 +412,11 @@ var _ = Describe("Crypto Setup TLS", func() {
)
Expect(clientErr).ToNot(HaveOccurred())
Expect(serverErr).ToNot(HaveOccurred())
Eventually(receivedSessionTicket).Should(BeClosed())
Eventually(csc.puts).Should(Receive())
Expect(server.ConnectionState().DidResume).To(BeFalse())
Expect(client.ConnectionState().DidResume).To(BeFalse())
serverConf.SessionTicketsDisabled = true
csc.EXPECT().Get(gomock.Any()).Return(state, true)
client, _, clientErr, server, _, serverErr = handshakeWithTLSConf(
clientConf, serverConf,
&utils.RTTStats{}, &utils.RTTStats{},
@@ -421,20 +425,13 @@ var _ = Describe("Crypto Setup TLS", func() {
)
Expect(clientErr).ToNot(HaveOccurred())
Expect(serverErr).ToNot(HaveOccurred())
Eventually(receivedSessionTicket).Should(BeClosed())
Consistently(csc.puts, 25*time.Millisecond).ShouldNot(Receive())
Expect(server.ConnectionState().DidResume).To(BeFalse())
Expect(client.ConnectionState().DidResume).To(BeFalse())
})
It("uses 0-RTT", func() {
csc := mocktls.NewMockClientSessionCache(mockCtrl)
var state *tls.ClientSessionState
receivedSessionTicket := make(chan struct{})
csc.EXPECT().Get(gomock.Any())
csc.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(_ string, css *tls.ClientSessionState) {
state = css
close(receivedSessionTicket)
})
csc := newMockClientSessionCache()
clientConf.ClientSessionCache = csc
const serverRTT = 25 * time.Millisecond // RTT as measured by the server. Should be restored.
const clientRTT = 30 * time.Millisecond // RTT as measured by the client. Should be restored.
@@ -450,13 +447,10 @@ var _ = Describe("Crypto Setup TLS", func() {
)
Expect(clientErr).ToNot(HaveOccurred())
Expect(serverErr).ToNot(HaveOccurred())
Eventually(receivedSessionTicket).Should(BeClosed())
Eventually(csc.puts).Should(Receive())
Expect(server.ConnectionState().DidResume).To(BeFalse())
Expect(client.ConnectionState().DidResume).To(BeFalse())
csc.EXPECT().Get(gomock.Any()).Return(state, true)
csc.EXPECT().Put(gomock.Any(), gomock.Any()).MaxTimes(1)
clientRTTStats := &utils.RTTStats{}
serverRTTStats := &utils.RTTStats{}
client, clientEvents, clientErr, server, serverEvents, serverErr := handshakeWithTLSConf(
@@ -501,14 +495,7 @@ var _ = Describe("Crypto Setup TLS", func() {
})
It("rejects 0-RTT, when the transport parameters changed", func() {
csc := mocktls.NewMockClientSessionCache(mockCtrl)
var state *tls.ClientSessionState
receivedSessionTicket := make(chan struct{})
csc.EXPECT().Get(gomock.Any())
csc.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(_ string, css *tls.ClientSessionState) {
state = css
close(receivedSessionTicket)
})
csc := newMockClientSessionCache()
clientConf.ClientSessionCache = csc
const clientRTT = 30 * time.Millisecond // RTT as measured by the client. Should be restored.
clientOrigRTTStats := newRTTStatsWithRTT(clientRTT)
@@ -522,13 +509,10 @@ var _ = Describe("Crypto Setup TLS", func() {
)
Expect(clientErr).ToNot(HaveOccurred())
Expect(serverErr).ToNot(HaveOccurred())
Eventually(receivedSessionTicket).Should(BeClosed())
Eventually(csc.puts).Should(Receive())
Expect(server.ConnectionState().DidResume).To(BeFalse())
Expect(client.ConnectionState().DidResume).To(BeFalse())
csc.EXPECT().Get(gomock.Any()).Return(state, true)
csc.EXPECT().Put(gomock.Any(), gomock.Any()).MaxTimes(1)
clientRTTStats := &utils.RTTStats{}
client, clientEvents, clientErr, server, _, serverErr := handshakeWithTLSConf(
clientConf, serverConf,

View File

@@ -13,7 +13,3 @@ package mocks
//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -package mocks -destination connection_flow_controller.go github.com/quic-go/quic-go/internal/flowcontrol ConnectionFlowController"
//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -package mockackhandler -destination ackhandler/sent_packet_handler.go github.com/quic-go/quic-go/internal/ackhandler SentPacketHandler"
//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -package mockackhandler -destination ackhandler/received_packet_handler.go github.com/quic-go/quic-go/internal/ackhandler ReceivedPacketHandler"
// The following command produces a warning message on OSX, however, it still generates the correct mock file.
// See https://github.com/golang/mock/issues/339 for details.
//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -package mocktls -destination tls/client_session_cache.go crypto/tls ClientSessionCache"

View File

@@ -1,115 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: crypto/tls (interfaces: ClientSessionCache)
//
// Generated by this command:
//
// mockgen -typed -build_flags=-tags=gomock -package mocktls -destination tls/client_session_cache.go crypto/tls ClientSessionCache
//
// Package mocktls is a generated GoMock package.
package mocktls
import (
tls "crypto/tls"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockClientSessionCache is a mock of ClientSessionCache interface.
type MockClientSessionCache struct {
ctrl *gomock.Controller
recorder *MockClientSessionCacheMockRecorder
}
// MockClientSessionCacheMockRecorder is the mock recorder for MockClientSessionCache.
type MockClientSessionCacheMockRecorder struct {
mock *MockClientSessionCache
}
// NewMockClientSessionCache creates a new mock instance.
func NewMockClientSessionCache(ctrl *gomock.Controller) *MockClientSessionCache {
mock := &MockClientSessionCache{ctrl: ctrl}
mock.recorder = &MockClientSessionCacheMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockClientSessionCache) EXPECT() *MockClientSessionCacheMockRecorder {
return m.recorder
}
// Get mocks base method.
func (m *MockClientSessionCache) Get(arg0 string) (*tls.ClientSessionState, bool) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Get", arg0)
ret0, _ := ret[0].(*tls.ClientSessionState)
ret1, _ := ret[1].(bool)
return ret0, ret1
}
// Get indicates an expected call of Get.
func (mr *MockClientSessionCacheMockRecorder) Get(arg0 any) *MockClientSessionCacheGetCall {
mr.mock.ctrl.T.Helper()
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockClientSessionCache)(nil).Get), arg0)
return &MockClientSessionCacheGetCall{Call: call}
}
// MockClientSessionCacheGetCall wrap *gomock.Call
type MockClientSessionCacheGetCall struct {
*gomock.Call
}
// Return rewrite *gomock.Call.Return
func (c *MockClientSessionCacheGetCall) Return(arg0 *tls.ClientSessionState, arg1 bool) *MockClientSessionCacheGetCall {
c.Call = c.Call.Return(arg0, arg1)
return c
}
// Do rewrite *gomock.Call.Do
func (c *MockClientSessionCacheGetCall) Do(f func(string) (*tls.ClientSessionState, bool)) *MockClientSessionCacheGetCall {
c.Call = c.Call.Do(f)
return c
}
// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockClientSessionCacheGetCall) DoAndReturn(f func(string) (*tls.ClientSessionState, bool)) *MockClientSessionCacheGetCall {
c.Call = c.Call.DoAndReturn(f)
return c
}
// Put mocks base method.
func (m *MockClientSessionCache) Put(arg0 string, arg1 *tls.ClientSessionState) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Put", arg0, arg1)
}
// Put indicates an expected call of Put.
func (mr *MockClientSessionCacheMockRecorder) Put(arg0, arg1 any) *MockClientSessionCachePutCall {
mr.mock.ctrl.T.Helper()
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Put", reflect.TypeOf((*MockClientSessionCache)(nil).Put), arg0, arg1)
return &MockClientSessionCachePutCall{Call: call}
}
// MockClientSessionCachePutCall wrap *gomock.Call
type MockClientSessionCachePutCall struct {
*gomock.Call
}
// Return rewrite *gomock.Call.Return
func (c *MockClientSessionCachePutCall) Return() *MockClientSessionCachePutCall {
c.Call = c.Call.Return()
return c
}
// Do rewrite *gomock.Call.Do
func (c *MockClientSessionCachePutCall) Do(f func(string, *tls.ClientSessionState)) *MockClientSessionCachePutCall {
c.Call = c.Call.Do(f)
return c
}
// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockClientSessionCachePutCall) DoAndReturn(f func(string, *tls.ClientSessionState)) *MockClientSessionCachePutCall {
c.Call = c.Call.DoAndReturn(f)
return c
}