Refactor smtp tests to improve clarity and error handling

Removed unused variables and improved error handling in smtp_test.go. Adjusted to capture only error in auth.Next() calls, ensuring accurate validation. Added necessary error checks after creating new client connections to prevent test failures.
This commit is contained in:
Winni Neessen 2024-11-11 18:35:10 +01:00
parent c58aa35454
commit 7da30e09e1
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -670,7 +670,7 @@ func TestLoginAuth(t *testing.T) {
if !bytes.Equal([]byte(pass), resp) { if !bytes.Equal([]byte(pass), resp) {
t.Errorf("expected response to second challange to be: %q, got: %q", pass, resp) t.Errorf("expected response to second challange to be: %q, got: %q", pass, resp)
} }
resp, err = auth.Next([]byte("nonsense"), true) _, err = auth.Next([]byte("nonsense"), true)
if err == nil { if err == nil {
t.Error("expected third server challange to fail, but didn't") t.Error("expected third server challange to fail, but didn't")
} }
@ -818,7 +818,7 @@ func TestLoginAuth_noEnc(t *testing.T) {
if !bytes.Equal([]byte(pass), resp) { if !bytes.Equal([]byte(pass), resp) {
t.Errorf("expected response to second challange to be: %q, got: %q", pass, resp) t.Errorf("expected response to second challange to be: %q, got: %q", pass, resp)
} }
resp, err = auth.Next([]byte("nonsense"), true) _, err = auth.Next([]byte("nonsense"), true)
if err == nil { if err == nil {
t.Error("expected third server challange to fail, but didn't") t.Error("expected third server challange to fail, but didn't")
} }
@ -910,7 +910,7 @@ func TestXOAuth2Auth(t *testing.T) {
if !bytes.Equal([]byte(""), resp) { if !bytes.Equal([]byte(""), resp) {
t.Errorf("expected server response to be empty, got: %q", resp) t.Errorf("expected server response to be empty, got: %q", resp)
} }
resp, err = auth.Next([]byte("nonsense"), false) _, err = auth.Next([]byte("nonsense"), false)
if err != nil { if err != nil {
t.Errorf("failed on first server challange: %s", err) t.Errorf("failed on first server challange: %s", err)
} }
@ -1107,6 +1107,9 @@ func TestScramAuth(t *testing.T) {
t.Fatalf("failed to dial TLS server: %v", err) t.Fatalf("failed to dial TLS server: %v", err)
} }
client, err = NewClient(conn, TestServerAddr) client, err = NewClient(conn, TestServerAddr)
if err != nil {
t.Fatalf("failed to connect to test server: %s", err)
}
case false: case false:
var err error var err error
client, err = Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort)) client, err = Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
@ -1176,6 +1179,9 @@ func TestScramAuth(t *testing.T) {
t.Fatalf("failed to dial TLS server: %v", err) t.Fatalf("failed to dial TLS server: %v", err)
} }
client, err = NewClient(conn, TestServerAddr) client, err = NewClient(conn, TestServerAddr)
if err != nil {
t.Fatalf("failed to connect to test server: %s", err)
}
case false: case false:
var err error var err error
client, err = Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort)) client, err = Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
@ -3434,7 +3440,7 @@ func TestClient_GetTLSConnectionState(t *testing.T) {
t.Fatalf("failed to get TLS connection state: %s", err) t.Fatalf("failed to get TLS connection state: %s", err)
} }
if state == nil { if state == nil {
t.Error("expected TLS connection state to be non-nil") t.Fatal("expected TLS connection state to be non-nil")
} }
if state.Version != tls.VersionTLS12 { if state.Version != tls.VersionTLS12 {
t.Errorf("expected TLS connection state version to be %d, got: %d", tls.VersionTLS12, state.Version) t.Errorf("expected TLS connection state version to be %d, got: %d", tls.VersionTLS12, state.Version)
@ -3543,7 +3549,6 @@ func TestClient_debugLog(t *testing.T) {
// faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes. // faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes.
type faker struct { type faker struct {
io.ReadWriter io.ReadWriter
failOnRead bool
failOnClose bool failOnClose bool
} }
@ -3865,7 +3870,6 @@ func handleTestServerConnection(connection net.Conn, t *testing.T, props *server
// fields are present. We have actual real authentication tests for all SCRAM modes in the // fields are present. We have actual real authentication tests for all SCRAM modes in the
// go-mail client_test.go // go-mail client_test.go
type testSCRAMSMTP struct { type testSCRAMSMTP struct {
authMechanism string
nonce string nonce string
h func() hash.Hash h func() hash.Hash
tlsServer bool tlsServer bool