From 72b3f53eb788979ea1bd8f9d85621f1d2d775280 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 1 Oct 2024 20:45:07 +0200 Subject: [PATCH] Add tests for unsupported SCRAM-SHA authentications Introduce a new test case `TestClient_AuthSCRAMSHAX_unsupported` to validate handling of unsupported SCRAM-SHA authentication methods. This ensures the client returns the correct errors when setting unsupported auth types. --- client_test.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/client_test.go b/client_test.go index 83d710c..481b9be 100644 --- a/client_test.go +++ b/client_test.go @@ -1905,6 +1905,41 @@ func TestClient_AuthSCRAMSHAX_fail(t *testing.T) { } } +func TestClient_AuthSCRAMSHAX_unsupported(t *testing.T) { + if os.Getenv("TEST_ALLOW_SEND") == "" { + t.Skipf("TEST_ALLOW_SEND is not set. Skipping mail sending test") + } + + client, err := getTestConnection(true) + if err != nil { + t.Skipf("failed to create test client: %s. Skipping tests", err) + } + + tests := []struct { + name string + authtype SMTPAuthType + expErr error + }{ + {"SCRAM-SHA-1", SMTPAuthSCRAMSHA1, ErrSCRAMSHA1AuthNotSupported}, + {"SCRAM-SHA-1-PLUS", SMTPAuthSCRAMSHA1PLUS, ErrSCRAMSHA1PLUSAuthNotSupported}, + {"SCRAM-SHA-256", SMTPAuthSCRAMSHA256, ErrSCRAMSHA256AuthNotSupported}, + {"SCRAM-SHA-256-PLUS", SMTPAuthSCRAMSHA256PLUS, ErrSCRAMSHA256PLUSAuthNotSupported}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client.SetSMTPAuth(tt.authtype) + client.SetTLSPolicy(TLSMandatory) + if err = client.DialWithContext(context.Background()); err == nil { + t.Errorf("expected error but got nil") + } + if !errors.Is(err, tt.expErr) { + t.Errorf("expected error %s, but got %s", tt.expErr, err) + } + }) + } +} + func TestClient_AuthSCRAMSHAXPLUS_tlsexporter(t *testing.T) { if os.Getenv("TEST_ONLINE_SCRAM") == "" { t.Skipf("TEST_ONLINE_SCRAM is not set. Skipping online SCRAM tests") @@ -2023,10 +2058,10 @@ func getTestConnection(auth bool) (*Client, error) { // 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) } - if err := c.Close(); err != nil { + if err = c.Close(); err != nil { return c, fmt.Errorf("disconnect from test server failed: %w", err) } return c, nil