From e36511e90ece616a78b3dc3bed4c7ea4e5a0fcf3 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 14 Jan 2023 13:05:04 +0100 Subject: [PATCH] Fix tests to not log authentication data We don't want to expose SMTP authentication details in tests, therefore the tests have been adjusted a bit --- client.go | 9 ++++++--- client_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++-- smtp/smtp.go | 10 +++++++--- smtp/smtp_test.go | 2 +- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index ab706b1..3f66b72 100644 --- a/client.go +++ b/client.go @@ -410,8 +410,11 @@ func (c *Client) SetSSL(s bool) { } // SetDebugLog tells the Client whether debug logging is enabled or not -func (c *Client) SetDebugLog(s bool) { - c.dl = s +func (c *Client) SetDebugLog(v bool) { + c.dl = v + if c.sc != nil { + c.sc.SetDebugLog(v) + } } // SetTLSConfig overrides the current *tls.Config with the given *tls.Config value @@ -479,7 +482,7 @@ func (c *Client) DialWithContext(pc context.Context) error { return err } if c.dl { - c.sc.SetDebugLog() + c.sc.SetDebugLog(true) } if err := c.sc.Hello(c.helo); err != nil { return err diff --git a/client_test.go b/client_test.go index 3e1e8ad..88000dc 100644 --- a/client_test.go +++ b/client_test.go @@ -546,11 +546,10 @@ func TestClient_DialWithContext(t *testing.T) { // TestClient_DialWithContext_Debug tests the DialWithContext method for the Client object with debug // logging enabled on the SMTP client func TestClient_DialWithContext_Debug(t *testing.T) { - c, err := getTestConnection(true) + c, err := getTestClient(true) if err != nil { t.Skipf("failed to create test client: %s. Skipping tests", err) } - c.SetDebugLog(true) ctx := context.Background() if err := c.DialWithContext(ctx); err != nil { t.Errorf("failed to dial with context: %s", err) @@ -562,6 +561,7 @@ func TestClient_DialWithContext_Debug(t *testing.T) { if c.sc == nil { t.Errorf("DialWithContext didn't fail but no SMTP client found.") } + c.SetDebugLog(true) if err := c.Close(); err != nil { t.Errorf("failed to close connection: %s", err) } @@ -1141,6 +1141,8 @@ func getTestConnection(auth bool) (*Client, error) { if p != "" { c.SetPassword(p) } + // We don't want to log authentication data in tests + c.SetDebugLog(false) } if err := c.DialWithContext(context.Background()); err != nil { return c, fmt.Errorf("connection to test server failed: %w", err) @@ -1151,6 +1153,51 @@ func getTestConnection(auth bool) (*Client, error) { return c, nil } +// getTestClient takes environment variables to establish a client without connecting +// to the SMTP server +func getTestClient(auth bool) (*Client, error) { + if os.Getenv("TEST_SKIP_ONLINE") != "" { + return nil, fmt.Errorf("env variable TEST_SKIP_ONLINE is set. Skipping online tests") + } + th := os.Getenv("TEST_HOST") + if th == "" { + return nil, fmt.Errorf("no TEST_HOST set") + } + tp := 25 + if tps := os.Getenv("TEST_PORT"); tps != "" { + tpi, err := strconv.Atoi(tps) + if err == nil { + tp = tpi + } + } + sv := false + if sve := os.Getenv("TEST_TLS_SKIP_VERIFY"); sve != "" { + sv = true + } + c, err := NewClient(th, WithPort(tp)) + if err != nil { + return c, err + } + c.tlsconfig.InsecureSkipVerify = sv + if auth { + st := os.Getenv("TEST_SMTPAUTH_TYPE") + if st != "" { + c.SetSMTPAuth(SMTPAuthType(st)) + } + u := os.Getenv("TEST_SMTPAUTH_USER") + if u != "" { + c.SetUsername(u) + } + p := os.Getenv("TEST_SMTPAUTH_PASS") + if p != "" { + c.SetPassword(p) + } + // We don't want to log authentication data in tests + c.SetDebugLog(false) + } + return c, nil +} + // getTestConnectionWithDSN takes environment variables to establish a connection to a real // SMTP server to test all functionality that requires a connection. It also enables DSN func getTestConnectionWithDSN(auth bool) (*Client, error) { diff --git a/smtp/smtp.go b/smtp/smtp.go index 7331a7f..7bdae59 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -432,9 +432,13 @@ func (c *Client) Quit() error { } // SetDebugLog enables the debug logging for incoming and outgoing SMTP messages -func (c *Client) SetDebugLog() { - c.debug = true - c.logger = log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmsgprefix) +func (c *Client) SetDebugLog(v bool) { + c.debug = v + if v { + c.logger = log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmsgprefix) + return + } + c.logger = nil } // debugLog checks if the debug flag is set and if so logs the provided message to StdErr diff --git a/smtp/smtp_test.go b/smtp/smtp_test.go index 26cf9ba..2dbfde8 100644 --- a/smtp/smtp_test.go +++ b/smtp/smtp_test.go @@ -655,7 +655,7 @@ func TestClient_SetDebugLog(t *testing.T) { defer func() { _ = c.Close() }() - c.SetDebugLog() + c.SetDebugLog(true) if !c.debug { t.Errorf("Expected DebugLog flag to be true but received false") }