From 9c40205343481628ea00165789188d5da4edfcb7 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Wed, 26 Feb 2025 15:41:46 +0100 Subject: [PATCH] imapserver: Prevent spurious test failures due to compression layer being closed and TLS close-writes failing --- imapclient/client.go | 10 ++++++++++ imapserver/compress_test.go | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/imapclient/client.go b/imapclient/client.go index b5a5513..e5ed9fb 100644 --- a/imapclient/client.go +++ b/imapclient/client.go @@ -170,10 +170,20 @@ func (c *Conn) xflush() { } } +// SetPanic sets whether errors cause a panic instead of returning errors. +func (c *Conn) SetPanic(panic bool) { + c.panic = panic +} + // Close closes the connection, flushing and closing any compression and TLS layer. // // You may want to call Logout first. Closing a connection with a mailbox with // deleted messages not yet expunged will not expunge those messages. +// +// Closing a TLS connection that is logged out, or closing a TLS connection with +// compression enabled (i.e. two layered streams), may cause spurious errors +// because the server may immediate close the underlying connection when it sees +// the connection is being closed. func (c *Conn) Close() (rerr error) { defer c.recover(&rerr) diff --git a/imapserver/compress_test.go b/imapserver/compress_test.go index 8f4c716..b8ec941 100644 --- a/imapserver/compress_test.go +++ b/imapserver/compress_test.go @@ -40,6 +40,11 @@ func TestCompressStartTLS(t *testing.T) { tc.transactf("ok", "append inbox (\\seen) {%d+}\r\n%s", len(exampleMsg), exampleMsg) tc.transactf("ok", "noop") tc.transactf("ok", "fetch 1 body.peek[1]") + + // Prevent client.Close from failing the test. The client first closes the + // compression stream, which causes the server to close the connection, after which + // the messages to close the TLS connection are written to a closed socket. + tc.client.SetPanic(false) } func TestCompressBreak(t *testing.T) {