From de099239339d4a7af5eb1d44692b95ce01ce8552 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Tue, 22 Nov 2016 22:05:07 +0000 Subject: [PATCH] smtpsrv: Disable SPF lookups in tests When testing, we don't want the server to do SPF lookups, as those cause real DNS queries which can be problematic and add a dependency on the environment. This patch adds an internal boolean to disable the SPF lookups, which is only set from the tests. --- internal/smtpsrv/conn.go | 7 +++++++ internal/smtpsrv/server_test.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/internal/smtpsrv/conn.go b/internal/smtpsrv/conn.go index 29920f4..4950322 100644 --- a/internal/smtpsrv/conn.go +++ b/internal/smtpsrv/conn.go @@ -48,6 +48,9 @@ var ( var ( maxReceivedHeaders = flag.Int("testing__max_received_headers", 50, "max Received headers, for loop detection; ONLY FOR TESTING") + + // Some go tests disable SPF, to avoid leaking DNS lookups. + disableSPFForTesting = false ) // Mode for a socket (listening or connection). @@ -385,6 +388,10 @@ func (c *Conn) checkSPF(addr string) (spf.Result, error) { return "", nil } + if disableSPFForTesting { + return "", nil + } + if tcp, ok := c.conn.RemoteAddr().(*net.TCPAddr); ok { res, err := spf.CheckHost( tcp.IP, envelope.DomainOf(addr)) diff --git a/internal/smtpsrv/server_test.go b/internal/smtpsrv/server_test.go index 5b0bb00..08d4512 100644 --- a/internal/smtpsrv/server_test.go +++ b/internal/smtpsrv/server_test.go @@ -453,6 +453,9 @@ func realMain(m *testing.M) int { s.AddDomain("localhost") s.AddUserDB("localhost", udb) + // Disable SPF lookups, to avoid leaking DNS queries. + disableSPFForTesting = true + go s.ListenAndServe() }