Refactor SMTP server port handling in tests

Modified tests to dynamically compute server ports from a base value, enhancing flexibility and preventing potential conflicts. Updated `simpleSMTPServer` function to accept a port parameter.
This commit is contained in:
Winni Neessen 2024-09-20 16:44:15 +02:00
parent bd5a8a40b9
commit 4ee11e8406
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -31,8 +31,8 @@ const (
TestServerProto = "tcp" TestServerProto = "tcp"
// TestServerAddr is the address the simple SMTP test server listens on // TestServerAddr is the address the simple SMTP test server listens on
TestServerAddr = "127.0.0.1" TestServerAddr = "127.0.0.1"
// TestServerPort is the port the simple SMTP test server listens on // TestServerPortBase is the base port for the simple SMTP test server
TestServerPort = 2526 TestServerPortBase = 2025
) )
// TestNewClient tests the NewClient() method with its default options // TestNewClient tests the NewClient() method with its default options
@ -1264,8 +1264,9 @@ func TestClient_SendErrorNoEncoding(t *testing.T) {
defer cancel() defer cancel()
featureSet := "250-AUTH PLAIN\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-DSN\r\n250 SMTPUTF8"
serverPort := TestServerPortBase + 1
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, false); err != nil { if err := simpleSMTPServer(ctx, featureSet, false, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1286,7 +1287,7 @@ func TestClient_SendErrorNoEncoding(t *testing.T) {
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
message.SetEncoding(NoEncoding) message.SetEncoding(NoEncoding)
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -1329,9 +1330,10 @@ func TestClient_SendErrorMailFrom(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
serverPort := TestServerPortBase + 2
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, false); err != nil { if err := simpleSMTPServer(ctx, featureSet, false, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1351,7 +1353,7 @@ func TestClient_SendErrorMailFrom(t *testing.T) {
message.SetBodyString(TypeTextPlain, "Test body") message.SetBodyString(TypeTextPlain, "Test body")
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -1394,9 +1396,10 @@ func TestClient_SendErrorMailFromReset(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
serverPort := TestServerPortBase + 3
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, true); err != nil { if err := simpleSMTPServer(ctx, featureSet, true, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1416,7 +1419,7 @@ func TestClient_SendErrorMailFromReset(t *testing.T) {
message.SetBodyString(TypeTextPlain, "Test body") message.SetBodyString(TypeTextPlain, "Test body")
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -1468,9 +1471,10 @@ func TestClient_SendErrorToReset(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
serverPort := TestServerPortBase + 4
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, true); err != nil { if err := simpleSMTPServer(ctx, featureSet, true, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1490,7 +1494,7 @@ func TestClient_SendErrorToReset(t *testing.T) {
message.SetBodyString(TypeTextPlain, "Test body") message.SetBodyString(TypeTextPlain, "Test body")
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -1542,9 +1546,10 @@ func TestClient_SendErrorDataClose(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
serverPort := TestServerPortBase + 5
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, false); err != nil { if err := simpleSMTPServer(ctx, featureSet, false, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1564,7 +1569,7 @@ func TestClient_SendErrorDataClose(t *testing.T) {
message.SetBodyString(TypeTextPlain, "DATA close should fail") message.SetBodyString(TypeTextPlain, "DATA close should fail")
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -1604,9 +1609,10 @@ func TestClient_SendErrorDataWrite(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
serverPort := TestServerPortBase + 6
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, false); err != nil { if err := simpleSMTPServer(ctx, featureSet, false, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1627,7 +1633,7 @@ func TestClient_SendErrorDataWrite(t *testing.T) {
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
message.SetGenHeader("X-Test-Header", "DATA write should fail") message.SetGenHeader("X-Test-Header", "DATA write should fail")
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -1657,19 +1663,16 @@ func TestClient_SendErrorDataWrite(t *testing.T) {
sendErr.MessageID()) sendErr.MessageID())
} }
} }
if err = client.Close(); err != nil {
t.Errorf("failed to close server connection: %s", err)
}
} }
func TestClient_SendErrorReset(t *testing.T) { func TestClient_SendErrorReset(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
serverPort := TestServerPortBase + 7
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() { go func() {
if err := simpleSMTPServer(ctx, featureSet, true); err != nil { if err := simpleSMTPServer(ctx, featureSet, true, serverPort); err != nil {
t.Errorf("failed to start test server: %s", err) t.Errorf("failed to start test server: %s", err)
return return
} }
@ -1689,7 +1692,7 @@ func TestClient_SendErrorReset(t *testing.T) {
message.SetBodyString(TypeTextPlain, "Test body") message.SetBodyString(TypeTextPlain, "Test body")
message.SetMessageIDWithValue("this.is.a.message.id") message.SetMessageIDWithValue("this.is.a.message.id")
client, err := NewClient(TestServerAddr, WithPort(TestServerPort), client, err := NewClient(TestServerAddr, WithPort(serverPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain), WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("toni@tester.com"), WithUsername("toni@tester.com"),
WithPassword("V3ryS3cr3t+")) WithPassword("V3ryS3cr3t+"))
@ -2023,8 +2026,8 @@ func (f faker) SetWriteDeadline(time.Time) error { return nil }
// simpleSMTPServer starts a simple TCP server that resonds to SMTP commands. // simpleSMTPServer starts a simple TCP server that resonds to SMTP commands.
// The provided featureSet represents in what the server responds to EHLO command // The provided featureSet represents in what the server responds to EHLO command
// failReset controls if a RSET succeeds // failReset controls if a RSET succeeds
func simpleSMTPServer(ctx context.Context, featureSet string, failReset bool) error { func simpleSMTPServer(ctx context.Context, featureSet string, failReset bool, port int) error {
listener, err := net.Listen(TestServerProto, fmt.Sprintf("%s:%d", TestServerAddr, TestServerPort)) listener, err := net.Listen(TestServerProto, fmt.Sprintf("%s:%d", TestServerAddr, port))
if err != nil { if err != nil {
return fmt.Errorf("unable to listen on %s://%s: %w", TestServerProto, TestServerAddr, err) return fmt.Errorf("unable to listen on %s://%s: %w", TestServerProto, TestServerAddr, err)
} }