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
This commit is contained in:
Winni Neessen 2023-01-14 13:05:04 +01:00
parent 34b432a985
commit e36511e90e
Signed by: wneessen
GPG key ID: 385AC9889632126E
4 changed files with 63 additions and 9 deletions

View file

@ -410,8 +410,11 @@ func (c *Client) SetSSL(s bool) {
} }
// SetDebugLog tells the Client whether debug logging is enabled or not // SetDebugLog tells the Client whether debug logging is enabled or not
func (c *Client) SetDebugLog(s bool) { func (c *Client) SetDebugLog(v bool) {
c.dl = s c.dl = v
if c.sc != nil {
c.sc.SetDebugLog(v)
}
} }
// SetTLSConfig overrides the current *tls.Config with the given *tls.Config value // 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 return err
} }
if c.dl { if c.dl {
c.sc.SetDebugLog() c.sc.SetDebugLog(true)
} }
if err := c.sc.Hello(c.helo); err != nil { if err := c.sc.Hello(c.helo); err != nil {
return err return err

View file

@ -546,11 +546,10 @@ func TestClient_DialWithContext(t *testing.T) {
// TestClient_DialWithContext_Debug tests the DialWithContext method for the Client object with debug // TestClient_DialWithContext_Debug tests the DialWithContext method for the Client object with debug
// logging enabled on the SMTP client // logging enabled on the SMTP client
func TestClient_DialWithContext_Debug(t *testing.T) { func TestClient_DialWithContext_Debug(t *testing.T) {
c, err := getTestConnection(true) c, err := getTestClient(true)
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.SetDebugLog(true)
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err != nil { if err := c.DialWithContext(ctx); err != nil {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
@ -562,6 +561,7 @@ func TestClient_DialWithContext_Debug(t *testing.T) {
if c.sc == nil { if c.sc == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
} }
c.SetDebugLog(true)
if err := c.Close(); err != nil { if err := c.Close(); err != nil {
t.Errorf("failed to close connection: %s", err) t.Errorf("failed to close connection: %s", err)
} }
@ -1141,6 +1141,8 @@ func getTestConnection(auth bool) (*Client, error) {
if p != "" { if p != "" {
c.SetPassword(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 { if err := c.DialWithContext(context.Background()); err != nil {
return c, fmt.Errorf("connection to test server failed: %w", err) return c, fmt.Errorf("connection to test server failed: %w", err)
@ -1151,6 +1153,51 @@ func getTestConnection(auth bool) (*Client, error) {
return c, nil 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 // 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 // SMTP server to test all functionality that requires a connection. It also enables DSN
func getTestConnectionWithDSN(auth bool) (*Client, error) { func getTestConnectionWithDSN(auth bool) (*Client, error) {

View file

@ -432,9 +432,13 @@ func (c *Client) Quit() error {
} }
// SetDebugLog enables the debug logging for incoming and outgoing SMTP messages // SetDebugLog enables the debug logging for incoming and outgoing SMTP messages
func (c *Client) SetDebugLog() { func (c *Client) SetDebugLog(v bool) {
c.debug = true c.debug = v
if v {
c.logger = log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmsgprefix) 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 // debugLog checks if the debug flag is set and if so logs the provided message to StdErr

View file

@ -655,7 +655,7 @@ func TestClient_SetDebugLog(t *testing.T) {
defer func() { defer func() {
_ = c.Close() _ = c.Close()
}() }()
c.SetDebugLog() c.SetDebugLog(true)
if !c.debug { if !c.debug {
t.Errorf("Expected DebugLog flag to be true but received false") t.Errorf("Expected DebugLog flag to be true but received false")
} }