Refactor SCRAM tests to include SHA-256-PLUS

Updated `TestClient_AuthSCRAMSHA1PLUS_tlsexporter` and `TestClient_AuthSCRAMSHA1PLUS_tlsunique` to test both SCRAM-SHA-1-PLUS and SCRAM-SHA-256-PLUS authentication types. Implemented table-driven tests to improve readability and maintainability.
This commit is contained in:
Winni Neessen 2024-10-01 16:43:36 +02:00
parent bcf7084982
commit 324be9d032
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -1836,7 +1836,7 @@ func TestClient_DialSendConcurrent_local(t *testing.T) {
}
}
func TestClient_AuthSCRAMSHA1PLUS_tlsexporter(t *testing.T) {
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")
}
@ -1844,45 +1844,71 @@ func TestClient_AuthSCRAMSHA1PLUS_tlsexporter(t *testing.T) {
username := os.Getenv("TEST_USER_SCRAM")
password := os.Getenv("TEST_PASS_SCRAM")
client, err := NewClient(hostname, WithTLSPortPolicy(TLSMandatory),
WithSMTPAuth(SMTPAuthSCRAMSHA1PLUS),
WithUsername(username), WithPassword(password))
if err != nil {
t.Errorf("unable to create new client: %s", err)
return
tests := []struct {
name string
authtype SMTPAuthType
}{
{"SCRAM-SHA-1-PLUS", SMTPAuthSCRAMSHA1PLUS},
{"SCRAM-SHA-256-PLUS", SMTPAuthSCRAMSHA256PLUS},
}
if err = client.DialWithContext(context.Background()); err != nil {
t.Errorf("failed to dial to test server: %s", err)
}
if err = client.Close(); err != nil {
t.Errorf("failed to close server connection: %s", err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(hostname,
WithTLSPortPolicy(TLSMandatory),
WithSMTPAuth(tt.authtype),
WithUsername(username), WithPassword(password))
if err != nil {
t.Errorf("unable to create new client: %s", err)
return
}
if err = client.DialWithContext(context.Background()); err != nil {
t.Errorf("failed to dial to test server: %s", err)
}
if err = client.Close(); err != nil {
t.Errorf("failed to close server connection: %s", err)
}
})
}
}
func TestClient_AuthSCRAMSHA1PLUS_tlsunique(t *testing.T) {
func TestClient_AuthSCRAMSHAXPLUS_tlsunique(t *testing.T) {
if os.Getenv("TEST_ONLINE_SCRAM") == "" {
t.Skipf("TEST_ONLINE_SCRAM is not set. Skipping online SCRAM tests")
}
hostname := os.Getenv("TEST_HOST_SCRAM")
username := os.Getenv("TEST_USER_SCRAM")
password := os.Getenv("TEST_PASS_SCRAM")
tlsConfig := &tls.Config{}
tlsConfig.MaxVersion = tls.VersionTLS12
tlsConfig.ServerName = hostname
client, err := NewClient(hostname, WithTLSPortPolicy(TLSMandatory),
WithTLSConfig(tlsConfig),
WithSMTPAuth(SMTPAuthSCRAMSHA1PLUS),
WithUsername(username), WithPassword(password))
if err != nil {
t.Errorf("unable to create new client: %s", err)
return
tests := []struct {
name string
authtype SMTPAuthType
}{
{"SCRAM-SHA-1-PLUS", SMTPAuthSCRAMSHA1PLUS},
{"SCRAM-SHA-256-PLUS", SMTPAuthSCRAMSHA256PLUS},
}
if err = client.DialWithContext(context.Background()); err != nil {
t.Errorf("failed to dial to test server: %s", err)
}
if err = client.Close(); err != nil {
t.Errorf("failed to close server connection: %s", err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(hostname,
WithTLSPortPolicy(TLSMandatory),
WithTLSConfig(tlsConfig),
WithSMTPAuth(tt.authtype),
WithUsername(username), WithPassword(password))
if err != nil {
t.Errorf("unable to create new client: %s", err)
return
}
if err = client.DialWithContext(context.Background()); err != nil {
t.Errorf("failed to dial to test server: %s", err)
}
if err = client.Close(); err != nil {
t.Errorf("failed to close server connection: %s", err)
}
})
}
}