make sure not to return closed session from Listener.Accept()

This commit is contained in:
Marten Seemann
2019-01-06 14:13:13 +07:00
parent 90514d53d1
commit 181aa493e0
3 changed files with 122 additions and 17 deletions

View File

@@ -147,23 +147,29 @@ var _ = Describe("Handshake tests", func() {
})
Context("rate limiting", func() {
It("rejects new connection attempts if connections don't get accepted", func() {
var server quic.Listener
dial := func() (quic.Session, error) {
return quic.DialAddr(
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
&tls.Config{RootCAs: testdata.GetRootCA()},
nil,
)
}
BeforeEach(func() {
serverConfig.AcceptCookie = func(net.Addr, *quic.Cookie) bool { return true }
var err error
// start the server, but don't call Accept
serverConfig.AcceptCookie = func(net.Addr, *quic.Cookie) bool {
return true
}
server, err := quic.ListenAddr("localhost:0", testdata.GetTLSConfig(), serverConfig)
server, err = quic.ListenAddr("localhost:0", testdata.GetTLSConfig(), serverConfig)
Expect(err).ToNot(HaveOccurred())
defer server.Close()
})
dial := func() (quic.Session, error) {
return quic.DialAddr(
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
&tls.Config{RootCAs: testdata.GetRootCA()},
nil,
)
}
AfterEach(func() {
Expect(server.Close()).To(Succeed())
})
It("rejects new connection attempts if connections don't get accepted", func() {
for i := 0; i < protocol.MaxAcceptQueueSize; i++ {
sess, err := dial()
Expect(err).ToNot(HaveOccurred())
@@ -171,7 +177,7 @@ var _ = Describe("Handshake tests", func() {
}
time.Sleep(25 * time.Millisecond) // wait a bit for the sessions to be queued
_, err = dial()
_, err := dial()
Expect(err).To(HaveOccurred())
// TODO(#1567): use the SERVER_BUSY error code
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.PeerGoingAway))
@@ -190,5 +196,37 @@ var _ = Describe("Handshake tests", func() {
// TODO(#1567): use the SERVER_BUSY error code
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.PeerGoingAway))
})
It("rejects new connection attempts if connections don't get accepted", func() {
firstSess, err := dial()
Expect(err).ToNot(HaveOccurred())
for i := 1; i < protocol.MaxAcceptQueueSize; i++ {
sess, err := dial()
Expect(err).ToNot(HaveOccurred())
defer sess.Close()
}
time.Sleep(25 * time.Millisecond) // wait a bit for the sessions to be queued
_, err = dial()
Expect(err).To(HaveOccurred())
// TODO(#1567): use the SERVER_BUSY error code
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.PeerGoingAway))
// Now close the one of the session that are waiting to be accepted.
// This should free one spot in the queue.
Expect(firstSess.Close())
time.Sleep(25 * time.Millisecond)
// dial again, and expect that this dial succeeds
_, err = dial()
Expect(err).ToNot(HaveOccurred())
time.Sleep(25 * time.Millisecond) // wait a bit for the session to be queued
_, err = dial()
// TODO(#1567): use the SERVER_BUSY error code
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.PeerGoingAway))
})
})
})