Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMTP client tests overhaul #361

Merged
merged 48 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a3fe2f8
Add test for MessageID on nil SendError
wneessen Nov 1, 2024
99c4378
Refactor and streamline authentication tests
wneessen Nov 1, 2024
3cfd205
Rename and expand `TestPlainAuth_noEnc` with additional checks
wneessen Nov 3, 2024
e42b022
Merge branch 'main' into smtp-client-tests
wneessen Nov 7, 2024
2391010
Rename parameter for consistency in auth functions
wneessen Nov 7, 2024
4103434
Refactor and expand TestLoginAuth
wneessen Nov 7, 2024
4221d48
Update login authentication test cases in smtp_test.go
wneessen Nov 7, 2024
b03fbb4
Add test server for SMTP authentication
wneessen Nov 7, 2024
7b8a24f
Merge branch 'main' into smtp-client-tests
wneessen Nov 8, 2024
1af17f1
Add cleanup to close client connections in tests
wneessen Nov 8, 2024
c656226
Add XOAuth2 authentication tests to SMTP package
wneessen Nov 8, 2024
d4c6cb5
Add SCRAM authentication tests to smtp package
wneessen Nov 8, 2024
d6f256c
Fix typo in error message in normalizeString function
wneessen Nov 8, 2024
a1efa1a
Remove redundant empty string check in SCRAM normalization
wneessen Nov 8, 2024
c6d416f
Fix typo in the tls-unique channel binding comment
wneessen Nov 8, 2024
92ab51b
Add comprehensive tests for scramAuth error handling
wneessen Nov 8, 2024
cefaa0d
Refactor SMTP test cases for improved clarity and coverage
wneessen Nov 9, 2024
af79644
Add test cases for invalid HELO/EHLO commands
wneessen Nov 9, 2024
50505e1
Add test for Client cmd failure on textproto command
wneessen Nov 9, 2024
8f28bab
Add tests for Client StartTLS functionality
wneessen Nov 9, 2024
b7ffce6
Add TLS connection state tests for SMTP client
wneessen Nov 9, 2024
0df2281
Add extensive client tests for Mail, Verify, and Auth commands
wneessen Nov 10, 2024
0d8d097
Add tests for DSN, 8BITMIME, and SMTPUTF8 support in SMTP
wneessen Nov 10, 2024
d446b49
Add client cleanup to SMTP tests and new TestClient_Rcpt
wneessen Nov 10, 2024
75bfdd2
Update smtp tests to use t.Fatalf for critical failures
wneessen Nov 11, 2024
e9c7bdb
Refactor TLS config initialization in tests
wneessen Nov 11, 2024
3fffcd1
Remove deprecated test hook for STARTTLS
wneessen Nov 11, 2024
c325262
Reverted change made in 3fffcd15f618a8196f90871eba8b51aeaa71f18f
wneessen Nov 11, 2024
c6da393
Add echo buffer to SMTP server tests
wneessen Nov 11, 2024
87accd2
Fix formatting in smtp_test.go and add new test cases
wneessen Nov 11, 2024
1bfec50
Add new SMTP test cases for various failure scenarios
wneessen Nov 11, 2024
2d384a7
Add unit tests for SMTP client extensions, reset, and noop
wneessen Nov 11, 2024
2cc6706
Remove obsolete SMTP client tests
wneessen Nov 11, 2024
59f2778
Add tests for Client's SetDebugLog method
wneessen Nov 11, 2024
9412f31
Lock mutex when setting the logger
wneessen Nov 11, 2024
5e3d14f
Add tests for Client's SetLogger method
wneessen Nov 11, 2024
8fbd94a
Add nil check in UpdateDeadline method
wneessen Nov 11, 2024
007d214
Add comprehensive tests for SMTP client functionality
wneessen Nov 11, 2024
3b9085e
Add tests for GetTLSConnectionState method in SMTP client
wneessen Nov 11, 2024
c58aa35
Add tests for Client debug logging behavior
wneessen Nov 11, 2024
7da30e0
Refactor smtp tests to improve clarity and error handling
wneessen Nov 11, 2024
1d19290
Merge branch 'main' into smtp-client-tests
wneessen Nov 11, 2024
61353d5
Fix variable declarations in test cases
wneessen Nov 11, 2024
2156fbc
Add mutex lock to handle concurrent SMTP test server connections
wneessen Nov 11, 2024
08034e6
Refactor echoBuffer parameter handling in tests
wneessen Nov 11, 2024
77175a2
Refactor and relocate JSON logger tests for Go 1.21 compliance
wneessen Nov 11, 2024
f7bdd8f
Refactor error variable in smtp test
wneessen Nov 11, 2024
800c266
Refactor test server code for thread safety
wneessen Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions senderror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ func TestSendError_MessageID(t *testing.T) {
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) {
Expand Down
4 changes: 2 additions & 2 deletions smtp/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type loginAuth struct {
// LoginAuth will only send the credentials if the connection is using TLS
// or is connected to localhost. Otherwise authentication will fail with an
// error, without sending the credentials.
func LoginAuth(username, password, host string, allowUnEnc bool) Auth {
return &loginAuth{username, password, host, 0, allowUnEnc}
func LoginAuth(username, password, host string, allowUnenc bool) Auth {
return &loginAuth{username, password, host, 0, allowUnenc}
}

// Start begins the SMTP authentication process by validating server's TLS status and hostname.
Expand Down
4 changes: 2 additions & 2 deletions smtp/auth_plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type plainAuth struct {
// PlainAuth will only send the credentials if the connection is using TLS
// or is connected to localhost. Otherwise authentication will fail with an
// error, without sending the credentials.
func PlainAuth(identity, username, password, host string, allowUnEnc bool) Auth {
return &plainAuth{identity, username, password, host, allowUnEnc}
func PlainAuth(identity, username, password, host string, allowUnenc bool) Auth {
return &plainAuth{identity, username, password, host, allowUnenc}
}

func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {
Expand Down
7 changes: 2 additions & 5 deletions smtp/auth_scram.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (a *scramAuth) initialClientMessage() ([]byte, error) {
connState := a.tlsConnState
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
// RFC9266:122 tls-unique not defined for tls 1.3 and later
if bindData == nil || connState.Version >= tls.VersionTLS13 {
Expand Down Expand Up @@ -308,10 +308,7 @@ func (a *scramAuth) normalizeUsername() (string, error) {
func (a *scramAuth) normalizeString(s string) (string, error) {
s, err := precis.OpaqueString.String(s)
if err != nil {
return "", fmt.Errorf("failled to normalize string: %w", err)
}
if s == "" {
return "", errors.New("normalized string is empty")
return "", fmt.Errorf("failed to normalize string: %w", err)
}
return s, nil
}
5 changes: 5 additions & 0 deletions smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,9 @@ func (c *Client) SetLogger(l log.Logger) {
if l == nil {
return
}
c.mutex.Lock()
c.logger = l
c.mutex.Unlock()
}

// SetLogAuthData enables logging of authentication data in the Client.
Expand Down Expand Up @@ -620,6 +622,9 @@ func (c *Client) HasConnection() bool {
func (c *Client) UpdateDeadline(timeout time.Duration) error {
c.mutex.Lock()
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 {
return fmt.Errorf("smtp: failed to update deadline: %w", err)
}
Expand Down
59 changes: 59 additions & 0 deletions smtp/smtp_121_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
Loading
Loading