Merge pull request #361 from wneessen/smtp-client-tests

SMTP client tests overhaul
This commit is contained in:
Winni Neessen 2024-11-11 19:57:31 +01:00 committed by GitHub
commit 580ef5ed48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 3732 additions and 2161 deletions

View file

@ -174,6 +174,12 @@ func TestSendError_MessageID(t *testing.T) {
t.Errorf("sendError expected empty message-id, got: %s", sendErr.MessageID()) t.Errorf("sendError expected empty message-id, got: %s", sendErr.MessageID())
} }
}) })
t.Run("TestSendError_MessageID on nil error should return empty", func(t *testing.T) {
var sendErr *SendError
if sendErr.MessageID() != "" {
t.Error("expected empty message-id on nil-senderror")
}
})
} }
func TestSendError_Msg(t *testing.T) { func TestSendError_Msg(t *testing.T) {

View file

@ -36,8 +36,8 @@ type loginAuth struct {
// LoginAuth will only send the credentials if the connection is using TLS // LoginAuth will only send the credentials if the connection is using TLS
// or is connected to localhost. Otherwise authentication will fail with an // or is connected to localhost. Otherwise authentication will fail with an
// error, without sending the credentials. // error, without sending the credentials.
func LoginAuth(username, password, host string, allowUnEnc bool) Auth { func LoginAuth(username, password, host string, allowUnenc bool) Auth {
return &loginAuth{username, password, host, 0, allowUnEnc} return &loginAuth{username, password, host, 0, allowUnenc}
} }
// Start begins the SMTP authentication process by validating server's TLS status and hostname. // Start begins the SMTP authentication process by validating server's TLS status and hostname.

View file

@ -28,8 +28,8 @@ type plainAuth struct {
// PlainAuth will only send the credentials if the connection is using TLS // PlainAuth will only send the credentials if the connection is using TLS
// or is connected to localhost. Otherwise authentication will fail with an // or is connected to localhost. Otherwise authentication will fail with an
// error, without sending the credentials. // error, without sending the credentials.
func PlainAuth(identity, username, password, host string, allowUnEnc bool) Auth { func PlainAuth(identity, username, password, host string, allowUnenc bool) Auth {
return &plainAuth{identity, username, password, host, allowUnEnc} return &plainAuth{identity, username, password, host, allowUnenc}
} }
func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) { func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {

View file

@ -154,7 +154,7 @@ func (a *scramAuth) initialClientMessage() ([]byte, error) {
connState := a.tlsConnState connState := a.tlsConnState
bindData := connState.TLSUnique bindData := connState.TLSUnique
// crypto/tl: no tls-unique channel binding value for this tls connection, possibly due to missing // crypto/tls: no tls-unique channel binding value for this tls connection, possibly due to missing
// extended master key support and/or resumed connection // extended master key support and/or resumed connection
// RFC9266:122 tls-unique not defined for tls 1.3 and later // RFC9266:122 tls-unique not defined for tls 1.3 and later
if bindData == nil || connState.Version >= tls.VersionTLS13 { if bindData == nil || connState.Version >= tls.VersionTLS13 {
@ -308,10 +308,7 @@ func (a *scramAuth) normalizeUsername() (string, error) {
func (a *scramAuth) normalizeString(s string) (string, error) { func (a *scramAuth) normalizeString(s string) (string, error) {
s, err := precis.OpaqueString.String(s) s, err := precis.OpaqueString.String(s)
if err != nil { if err != nil {
return "", fmt.Errorf("failled to normalize string: %w", err) return "", fmt.Errorf("failed to normalize string: %w", err)
}
if s == "" {
return "", errors.New("normalized string is empty")
} }
return s, nil return s, nil
} }

View file

@ -587,7 +587,9 @@ func (c *Client) SetLogger(l log.Logger) {
if l == nil { if l == nil {
return return
} }
c.mutex.Lock()
c.logger = l c.logger = l
c.mutex.Unlock()
} }
// SetLogAuthData enables logging of authentication data in the Client. // SetLogAuthData enables logging of authentication data in the Client.
@ -620,6 +622,9 @@ func (c *Client) HasConnection() bool {
func (c *Client) UpdateDeadline(timeout time.Duration) error { func (c *Client) UpdateDeadline(timeout time.Duration) error {
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()
if c.conn == nil {
return errors.New("smtp: client has no connection")
}
if err := c.conn.SetDeadline(time.Now().Add(timeout)); err != nil { if err := c.conn.SetDeadline(time.Now().Add(timeout)); err != nil {
return fmt.Errorf("smtp: failed to update deadline: %w", err) return fmt.Errorf("smtp: failed to update deadline: %w", err)
} }

59
smtp/smtp_121_test.go Normal file
View file

@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: Copyright 2010 The Go Authors. All rights reserved.
// SPDX-FileCopyrightText: Copyright (c) 2022-2023 The go-mail Authors
//
// Original net/smtp code from the Go stdlib by the Go Authors.
// Use of this source code is governed by a BSD-style
// LICENSE file that can be found in this directory.
//
// go-mail specific modifications by the go-mail Authors.
// Licensed under the MIT License.
// See [PROJECT ROOT]/LICENSES directory for more information.
//
// SPDX-License-Identifier: BSD-3-Clause AND MIT
//go:build go1.21
// +build go1.21
package smtp
import (
"fmt"
"os"
"strings"
"testing"
"github.com/wneessen/go-mail/log"
)
func TestClient_SetDebugLog_JSON(t *testing.T) {
t.Run("set debug loggging to on should not override logger", func(t *testing.T) {
client := &Client{logger: log.NewJSON(os.Stderr, log.LevelDebug)}
client.SetDebugLog(true)
if !client.debug {
t.Fatalf("expected debug log to be true")
}
if client.logger == nil {
t.Fatalf("expected logger to be defined")
}
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.JSONlog") {
t.Errorf("expected logger to be of type *log.JSONlog, got: %T", client.logger)
}
})
}
func TestClient_SetLogger_JSON(t *testing.T) {
t.Run("set logger to JSONlog logger", func(t *testing.T) {
client := &Client{}
client.SetLogger(log.NewJSON(os.Stderr, log.LevelDebug))
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.JSONlog") {
t.Errorf("expected logger to be of type *log.JSONlog, got: %T", client.logger)
}
})
t.Run("nil logger should just return and not set/override", func(t *testing.T) {
client := &Client{logger: log.NewJSON(os.Stderr, log.LevelDebug)}
client.SetLogger(nil)
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.JSONlog") {
t.Errorf("expected logger to be of type *log.JSONlog, got: %T", client.logger)
}
})
}

File diff suppressed because it is too large Load diff