From 0d8d097ae1a5ac8ed739abb2253e53606baa8ade Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 10 Nov 2024 14:10:50 +0100 Subject: [PATCH] Add tests for DSN, 8BITMIME, and SMTPUTF8 support in SMTP Introduce new test cases to verify the SMTP server's ability to handle DSN, 8BITMIME, and SMTPUTF8 features. These tests ensure correct response behavior when these features are supported by the server. --- smtp/smtp_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/smtp/smtp_test.go b/smtp/smtp_test.go index a6185d5..bf31b05 100644 --- a/smtp/smtp_test.go +++ b/smtp/smtp_test.go @@ -2266,6 +2266,72 @@ func TestClient_Mail(t *testing.T) { t.Errorf("expected mail from command to be %q, but sent %q", expected, sent) } }) + t.Run("from address and server supports DSN", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + PortAdder.Add(1) + serverPort := int(TestServerPortBase + PortAdder.Load()) + featureSet := "250-DSN\r\n250 STARTTLS" + go func() { + if err := simpleSMTPServer(ctx, t, &serverProps{ + EchoCommandAsError: "MAIL FROM:", + FeatureSet: featureSet, + ListenPort: serverPort, + }, + ); err != nil { + t.Errorf("failed to start test server: %s", err) + return + } + }() + time.Sleep(time.Millisecond * 30) + + client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort)) + if err != nil { + t.Errorf("failed to dial to test server: %s", err) + } + client.dsnmrtype = "FULL" + if err = client.Mail("valid-from@domain.tld"); err == nil { + t.Error("server should echo the command as error but didn't") + } + sent := strings.Replace(err.Error(), "500 ", "", -1) + expected := "MAIL FROM: RET=FULL" + if !strings.EqualFold(sent, expected) { + t.Errorf("expected mail from command to be %q, but sent %q", expected, sent) + } + }) + t.Run("from address and server supports DSN, SMTPUTF8 and 8BITMIME", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + PortAdder.Add(1) + serverPort := int(TestServerPortBase + PortAdder.Load()) + featureSet := "250-DSN\r\n250-8BITMIME\r\n250-SMTPUTF8\r\n250 STARTTLS" + go func() { + if err := simpleSMTPServer(ctx, t, &serverProps{ + EchoCommandAsError: "MAIL FROM:", + FeatureSet: featureSet, + ListenPort: serverPort, + }, + ); err != nil { + t.Errorf("failed to start test server: %s", err) + return + } + }() + time.Sleep(time.Millisecond * 30) + + client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort)) + if err != nil { + t.Errorf("failed to dial to test server: %s", err) + } + client.dsnmrtype = "FULL" + if err = client.Mail("valid-from@domain.tld"); err == nil { + t.Error("server should echo the command as error but didn't") + } + sent := strings.Replace(err.Error(), "500 ", "", -1) + expected := "MAIL FROM: BODY=8BITMIME SMTPUTF8 RET=FULL" + if !strings.EqualFold(sent, expected) { + t.Errorf("expected mail from command to be %q, but sent %q", expected, sent) + } + }) } /*